Skip to content

Commit 0702fbb

Browse files
committed
Do not save confi in file cache
1 parent 1a40e5a commit 0702fbb

File tree

10 files changed

+105
-110
lines changed

10 files changed

+105
-110
lines changed

packages/data-context/src/DataContext.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { NexusGenAbstractTypeMembers } from '@packages/graphql/src/gen/nxs.
55
import type { AuthApiShape } from './actions/AuthActions'
66
import debugLib from 'debug'
77
import fsExtra from 'fs-extra'
8-
import { CoreDataShape, makeCoreData } from './data/coreDataShape'
98
import { DataActions } from './DataActions'
109
import {
1110
AppDataSource,
@@ -27,21 +26,15 @@ export interface DataContextConfig extends DataContextShellConfig {
2726
os: PlatformName
2827
launchArgs: LaunchArgs
2928
launchOptions: OpenProjectLaunchOptions
30-
/**
31-
* Default is to
32-
*/
33-
coreData?: CoreDataShape
3429
/**
3530
* Injected from the server
3631
*/
3732
appApi: AppApiShape
3833
authApi: AuthApiShape
39-
projectApi: ProjectApiShape
34+
projectApi: (ctx: DataContext) => ProjectApiShape
4035
}
4136

4237
export class DataContext extends DataContextShell {
43-
private _coreData: CoreDataShape
44-
4538
@cached
4639
get fs () {
4740
return fsExtra
@@ -54,7 +47,6 @@ export class DataContext extends DataContextShell {
5447

5548
constructor (private config: DataContextConfig) {
5649
super(config)
57-
this._coreData = config.coreData ?? makeCoreData()
5850
}
5951

6052
async initializeData () {
@@ -98,10 +90,6 @@ export class DataContext extends DataContextShell {
9890
return this.config.launchOptions
9991
}
10092

101-
get coreData () {
102-
return this._coreData
103-
}
104-
10593
get user () {
10694
return this.coreData.user
10795
}
@@ -188,7 +176,7 @@ export class DataContext extends DataContextShell {
188176
return {
189177
appApi: this.config.appApi,
190178
authApi: this.config.authApi,
191-
projectApi: this.config.projectApi,
179+
projectApi: this.config.projectApi(this),
192180
busApi: this.config.rootBus,
193181
}
194182
}

packages/data-context/src/DataContextShell.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import type { Server } from 'http'
33
import type { AddressInfo } from 'net'
44
import { DataEmitterActions } from './actions/DataEmitterActions'
55
import { cached } from './util/cached'
6+
import { CoreDataShape, makeCoreData } from './data/coreDataShape'
67

78
export interface DataContextShellConfig {
89
rootBus: EventEmitter
10+
/**
11+
* Default is to
12+
*/
13+
coreData?: CoreDataShape
914
}
1015

1116
// Used in places where we have to create a "shell" data context,
@@ -14,8 +19,11 @@ export class DataContextShell {
1419
private _gqlServer?: Server
1520
private _appServerPort: number | undefined
1621
private _gqlServerPort: number | undefined
22+
private _coreData: CoreDataShape
1723

18-
constructor (private shellConfig: DataContextShellConfig = { rootBus: new EventEmitter }) {}
24+
constructor (private shellConfig: DataContextShellConfig = { rootBus: new EventEmitter }) {
25+
this._coreData = shellConfig.coreData ?? makeCoreData()
26+
}
1927

2028
setAppServerPort (port: number | undefined) {
2129
this._appServerPort = port
@@ -45,6 +53,10 @@ export class DataContextShell {
4553
}
4654
}
4755

56+
get coreData () {
57+
return this._coreData
58+
}
59+
4860
destroy () {
4961
this._gqlServer?.close()
5062
}

packages/data-context/src/actions/ProjectActions.ts

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export interface ProjectApiShape {
2525
clearProjectPreferences(projectTitle: string): Promise<unknown>
2626
clearAllProjectPreferences(): Promise<unknown>
2727
closeActiveProject(): Promise<unknown>
28-
getProjectConfig (projectRoot: string): Promise<FullConfig | null>
2928
error(type: string, ...args: any): Error
3029
}
3130

packages/data-context/src/data/coreDataShape.ts

+9
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,29 @@ export interface BaseErrorDataShape {
6363
stack?: string
6464
}
6565

66+
export interface ActiveProjectConfig {
67+
settings: Cypress.ConfigOptions
68+
envFile: Cypress.ObjectLike
69+
}
70+
6671
export interface CoreDataShape {
6772
baseError: BaseErrorDataShape | null
6873
dev: DevStateShape
6974
app: AppDataShape
7075
wizard: WizardDataShape
7176
user: AuthenticatedUserShape | null
7277
electron: ElectronShape
78+
activeProjectConfig: {
79+
[key: string]: ActiveProjectConfig | null
80+
}
7381
}
7482

7583
/**
7684
* All state for the app should live here for now
7785
*/
7886
export function makeCoreData (): CoreDataShape {
7987
return {
88+
activeProjectConfig: {},
8089
baseError: null,
8190
dev: {
8291
refreshState: null,

packages/data-context/src/sources/ProjectDataSource.ts

-7
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ export class ProjectDataSource {
8888
// Check first the config files, to be sure there are no 2 config files
8989
const configFile = await this.getDefaultConfigFilePath(projectRoot)
9090

91-
// Check if we have already cached the config
92-
const config = await this.api.getProjectConfig(projectRoot)
93-
94-
if (config) {
95-
return config
96-
}
97-
9891
return this.configLoader({
9992
configFile,
10093
}).load(projectRoot)

packages/server/lib/cache.js

-13
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,6 @@ module.exports = {
145145
return fileUtil.set({ USER: {} })
146146
},
147147

148-
// We may need to set this value every time the config file is read, or updated
149-
// having it on the cache, will help us preventing to read the file on a cp
150-
// to get the projectId
151-
setProjectConfig (projectRoot, config) {
152-
return fileUtil.set({ PROJECTS_CONFIG: { [projectRoot]: config } })
153-
},
154-
155-
getProjectConfig (projectRoot) {
156-
return fileUtil.get('PROJECTS_CONFIG').then((projects) => {
157-
return projects && projects[projectRoot] ? projects[projectRoot] : null
158-
})
159-
},
160-
161148
removeLatestProjects () {
162149
return fileUtil.set({ PROJECTS: [] })
163150
},

packages/server/lib/config.ts

+31-15
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import * as settings from './util/settings'
1414
import Debug from 'debug'
1515
import pathHelpers from './util/path_helpers'
1616
import findSystemNode from './util/find_system_node'
17-
import { setProjectConfig } from './cache'
1817

1918
export interface ConfigSettingsConfig {
2019
testingType: TestingType
@@ -25,6 +24,7 @@ const debug = Debug('cypress:server:config')
2524

2625
import { options, breakingOptions } from './config_options'
2726
import { getProcessEnvVars } from './util/config'
27+
import type { DataContextShell } from '@packages/data-context/src/DataContextShell'
2828

2929
export const CYPRESS_ENV_PREFIX = 'CYPRESS_'
3030

@@ -204,12 +204,29 @@ export type FullConfig =
204204
resolved: ResolvedConfigurationOptions
205205
}
206206

207-
export function get (projectRoot, options: {configFile?: string | false } = { configFile: undefined }): Promise<FullConfig> {
208-
return Promise.all([
209-
settings.read(projectRoot, options).then(validateFile(options.configFile ?? 'cypress.config.{ts|js}')),
210-
settings.readEnv(projectRoot).then(validateFile('cypress.env.json')),
211-
])
207+
export function get (projectRoot, options: {configFile?: string | false } = { configFile: undefined }, ctx?: DataContextShell): Promise<FullConfig> {
208+
return Promise.resolve(ctx?.coreData.activeProjectConfig[projectRoot])
209+
.then((config) => {
210+
if (config) {
211+
return Promise.resolve([config.settings, config.envFile])
212+
}
213+
214+
return Promise.all([
215+
settings.read(projectRoot, options).then(validateFile(options.configFile ?? 'cypress.config.{ts|js}')),
216+
settings.readEnv(projectRoot).then(validateFile('cypress.env.json')),
217+
])
218+
})
212219
.spread((settings, envFile) => {
220+
if (ctx) {
221+
ctx.coreData.activeProjectConfig = {
222+
...ctx.coreData.activeProjectConfig,
223+
[projectRoot]: {
224+
settings,
225+
envFile,
226+
},
227+
}
228+
}
229+
213230
return set({
214231
projectName: getNameFromRoot(projectRoot),
215232
projectRoot,
@@ -218,17 +235,16 @@ export function get (projectRoot, options: {configFile?: string | false } = { co
218235
options,
219236
})
220237
})
221-
.then((fullConfig) => {
222-
return setProjectConfig(projectRoot, fullConfig).then(() => {
223-
return fullConfig
224-
})
225-
})
226238
.catch((e) => {
227239
// Cleanup the cache if there's an error to prevent showing stale data
228-
return setProjectConfig(projectRoot, null)
229-
.then(() => {
230-
throw e
231-
})
240+
if (ctx?.coreData.activeProjectConfig) {
241+
ctx.coreData.activeProjectConfig = {
242+
...ctx.coreData.activeProjectConfig,
243+
[projectRoot]: null,
244+
}
245+
}
246+
247+
throw e
232248
})
233249
}
234250

packages/server/lib/makeDataContext.ts

+45-46
Original file line numberDiff line numberDiff line change
@@ -38,52 +38,51 @@ export function makeDataContext (options: MakeDataContextOptions) {
3838
return user.logOut()
3939
},
4040
},
41-
projectApi: {
42-
getConfig (projectRoot: string, options?: SettingsOptions) {
43-
return config.get(projectRoot, options)
44-
},
45-
launchProject (browser: FoundBrowser, spec: Cypress.Spec, options?: LaunchOpts) {
46-
return openProject.launch({ ...browser }, spec, options)
47-
},
48-
initializeProject (args: LaunchArgs, options: OpenProjectLaunchOptions, browsers: FoundBrowser[]) {
49-
return openProject.create(args.projectRoot, args, options, browsers)
50-
},
51-
insertProjectToCache (projectRoot: string) {
52-
cache.insertProject(projectRoot)
53-
},
54-
getProjectRootsFromCache () {
55-
return cache.getProjectRoots()
56-
},
57-
findSpecs (payload: FindSpecs) {
58-
return specsUtil.findSpecs(payload)
59-
},
60-
getProjectConfig (projectRoot: string) {
61-
return cache.getProjectConfig(projectRoot)
62-
},
63-
clearLatestProjectsCache () {
64-
return cache.removeLatestProjects()
65-
},
66-
getProjectPreferencesFromCache () {
67-
return cache.getProjectPreferences()
68-
},
69-
clearProjectPreferences (projectTitle: string) {
70-
return cache.removeProjectPreferences(projectTitle)
71-
},
72-
clearAllProjectPreferences () {
73-
return cache.removeAllProjectPreferences()
74-
},
75-
insertProjectPreferencesToCache (projectTitle: string, preferences: Preferences) {
76-
cache.insertProjectPreferences(projectTitle, preferences)
77-
},
78-
removeProjectFromCache (path: string) {
79-
return cache.removeProject(path)
80-
},
81-
closeActiveProject () {
82-
return openProject.closeActiveProject()
83-
},
84-
error (type: string, ...args: any) {
85-
throw errors.throw(type, ...args)
86-
},
41+
projectApi (ctx: DataContext) {
42+
return {
43+
getConfig (projectRoot: string, options?: SettingsOptions) {
44+
return config.get(projectRoot, options, ctx)
45+
},
46+
launchProject (browser: FoundBrowser, spec: Cypress.Spec, options?: LaunchOpts) {
47+
return openProject.launch({ ...browser }, spec, options)
48+
},
49+
initializeProject (args: LaunchArgs, options: OpenProjectLaunchOptions, browsers: FoundBrowser[]) {
50+
return openProject.create(args.projectRoot, args, options, browsers)
51+
},
52+
insertProjectToCache (projectRoot: string) {
53+
cache.insertProject(projectRoot)
54+
},
55+
getProjectRootsFromCache () {
56+
return cache.getProjectRoots()
57+
},
58+
findSpecs (payload: FindSpecs) {
59+
return specsUtil.findSpecs(payload)
60+
},
61+
clearLatestProjectsCache () {
62+
return cache.removeLatestProjects()
63+
},
64+
getProjectPreferencesFromCache () {
65+
return cache.getProjectPreferences()
66+
},
67+
clearProjectPreferences (projectTitle: string) {
68+
return cache.removeProjectPreferences(projectTitle)
69+
},
70+
clearAllProjectPreferences () {
71+
return cache.removeAllProjectPreferences()
72+
},
73+
insertProjectPreferencesToCache (projectTitle: string, preferences: Preferences) {
74+
cache.insertProjectPreferences(projectTitle, preferences)
75+
},
76+
removeProjectFromCache (path: string) {
77+
return cache.removeProject(path)
78+
},
79+
closeActiveProject () {
80+
return openProject.closeActiveProject()
81+
},
82+
error (type: string, ...args: any) {
83+
throw errors.throw(type, ...args)
84+
},
85+
}
8786
},
8887
})
8988
}

packages/server/lib/project-base.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import { SpecsStore } from './specs-store'
3434
import { checkSupportFile, getDefaultConfigFilePath } from './project_utils'
3535
import type { FoundBrowser, OpenProjectLaunchOptions } from '@packages/types'
3636
import { DataContextShell } from '@packages/data-context/src/DataContextShell'
37-
import { getProjectConfig } from './cache'
3837

3938
// Cannot just use RuntimeConfigOptions as is because some types are not complete.
4039
// Instead, this is an interface of values that have been manually validated to exist
@@ -706,7 +705,7 @@ export class ProjectBase<TServer extends Server> extends EE {
706705
this.options.configFile = await getDefaultConfigFilePath(this.projectRoot)
707706
}
708707

709-
let theCfg: Cfg = await config.get(this.projectRoot, this.options)
708+
let theCfg: Cfg = await config.get(this.projectRoot, this.options, this.ctx)
710709

711710
if (!theCfg.browsers || theCfg.browsers.length === 0) {
712711
// @ts-ignore - we don't know if the browser is headed or headless at this point.
@@ -861,10 +860,10 @@ export class ProjectBase<TServer extends Server> extends EE {
861860
async getProjectId () {
862861
await this.verifyExistence()
863862

864-
const config = await getProjectConfig(this.projectRoot)
863+
const config = this.ctx.coreData.activeProjectConfig[this.projectRoot]
865864

866-
if (config?.projectId) {
867-
return config.projectId
865+
if (config?.settings.projectId) {
866+
return config.settings.projectId
868867
}
869868

870869
const readSettings = await settings.read(this.projectRoot, this.options)

packages/server/lib/util/settings.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { fs } from '../util/fs'
66
import { requireAsync } from './require_async'
77
import Debug from 'debug'
88
import type { SettingsOptions } from '@packages/types'
9-
import { getProjectConfig } from '../cache'
109

1110
const debug = Debug('cypress:server:settings')
1211

@@ -131,13 +130,7 @@ export function configFile (options: SettingsOptions = {}) {
131130
}
132131

133132
export function id (projectRoot, options = {}) {
134-
return getProjectConfig(projectRoot).then((config) => {
135-
if (config) {
136-
return Promise.resolve(config)
137-
}
138-
139-
return read(projectRoot, options)
140-
})
133+
return read(projectRoot, options)
141134
.then((config) => config.projectId)
142135
.catch(() => {
143136
return null

0 commit comments

Comments
 (0)