Skip to content

Commit 6ccafa1

Browse files
CoderCococlaude
andcommitted
fix(config): use app.isPackaged instead of resourcesPath to detect packaged build
process.resourcesPath is truthy in Electron dev mode too, making the dev fallback in getTfStatePath() unreachable. Switch to app.isPackaged (via a new readIsPackaged() helper, following the same lazy-require pattern as readUserDataPath()) so the packaged-path branch only fires in a real packaged build. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 898eca1 commit 6ccafa1

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

app/packages/desktop-main/src/services/ConfigService.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,17 @@ describe('ConfigService', () => {
254254
delete process.env['SERVER_CONFIG_PATH'];
255255
});
256256

257-
it('should return packaged tfstate path when readResourcesPath returns a value', () => {
258-
vi.spyOn(service as unknown as { readResourcesPath: () => string | undefined }, 'readResourcesPath').mockReturnValue('/fake/resources');
257+
it('should return packaged tfstate path when readIsPackaged returns true', () => {
258+
type Internals = { readIsPackaged: () => boolean; readResourcesPath: () => string | undefined };
259+
vi.spyOn(service as unknown as Internals, 'readIsPackaged').mockReturnValue(true);
260+
vi.spyOn(service as unknown as Internals, 'readResourcesPath').mockReturnValue('/fake/resources');
259261
expect(service.getTfStatePath()).toBe(
260262
path.join('/fake/resources', 'terraform', 'aws', 'terraform.tfstate'),
261263
);
262264
});
263265

264-
it('should return the repo-relative fallback when readResourcesPath returns undefined', () => {
265-
vi.spyOn(service as unknown as { readResourcesPath: () => string | undefined }, 'readResourcesPath').mockReturnValue(undefined);
266+
it('should return the repo-relative fallback when readIsPackaged returns false', () => {
267+
vi.spyOn(service as unknown as { readIsPackaged: () => boolean }, 'readIsPackaged').mockReturnValue(false);
266268
const result = service.getTfStatePath();
267269
expect(result).toMatch(/terraform[/\\]terraform\.tfstate$/);
268270
expect(path.isAbsolute(result)).toBe(true);

app/packages/desktop-main/src/services/ConfigService.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,24 @@ export class ConfigService {
174174
return (process as NodeJS.Process & { resourcesPath?: string }).resourcesPath;
175175
}
176176

177+
/**
178+
* Return whether the app is running as a packaged Electron build
179+
* (`electron.app.isPackaged`). `process.resourcesPath` is set in both dev
180+
* and packaged Electron processes, so it cannot be used as a packaged-build
181+
* guard — this method is the reliable alternative. Extracted as a protected
182+
* method so tests can stub it via `vi.spyOn`.
183+
*/
184+
protected readIsPackaged(): boolean {
185+
if (!process.versions['electron']) return false;
186+
try {
187+
const _require = createRequire(import.meta.url);
188+
const electron = _require('electron') as { app: { isPackaged: boolean } };
189+
return electron.app.isPackaged;
190+
} catch {
191+
return false;
192+
}
193+
}
194+
177195
/**
178196
* Return the Electron `userData` directory when running inside an Electron
179197
* process, or `null` otherwise. The `electron` module is required lazily at
@@ -198,16 +216,16 @@ export class ConfigService {
198216
*
199217
* Resolution order:
200218
* 1. `TF_STATE_PATH` env var — wins when set.
201-
* 2. Electron packaged app — `<resourcesPath>/terraform/aws/terraform.tfstate`.
219+
* 2. Electron packaged app (`app.isPackaged`) — `<resourcesPath>/terraform/aws/terraform.tfstate`.
202220
* 3. Dev/test fallback — repo root `terraform/terraform.tfstate`.
203221
*/
204222
getTfStatePath(): string {
205223
const envOverride = process.env['TF_STATE_PATH'];
206224
if (envOverride) return envOverride;
207225

208-
const resourcesPath = this.readResourcesPath();
209-
if (resourcesPath) {
210-
return join(resourcesPath, 'terraform', 'aws', 'terraform.tfstate');
226+
if (this.readIsPackaged()) {
227+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
228+
return join(this.readResourcesPath()!, 'terraform', 'aws', 'terraform.tfstate');
211229
}
212230

213231
// Dev fallback: repo root is one level above _APP_ROOT (app/)

0 commit comments

Comments
 (0)