diff --git a/.gitignore b/.gitignore index 11a7486bf533c..95f843584c0e4 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ test-results/ yarn-error.log vscode.lsif vscode.db +/.profile-oss diff --git a/.vscode/launch.json b/.vscode/launch.json index 4e450e9e69659..50cd6134a361c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -219,7 +219,7 @@ "cascadeTerminateToConfigurations": [ "Attach to Extension Host" ], - "userDataDir": false, + "userDataDir": "${workspaceFolder}/.profile-oss", "pauseForSourceMap": false, "outFiles": [ "${workspaceFolder}/out/**/*.js" diff --git a/.vscode/settings.json b/.vscode/settings.json index a97841683c0ef..2c8dce10702a6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "files.exclude": { ".git": true, ".build": true, + ".profile-oss": true, "**/.DS_Store": true, "build/**/*.js": { "when": "$(basename).ts" diff --git a/src/vs/code/electron-main/main.ts b/src/vs/code/electron-main/main.ts index c3477d25bc751..34f6d6506b34a 100644 --- a/src/vs/code/electron-main/main.ts +++ b/src/vs/code/electron-main/main.ts @@ -59,6 +59,7 @@ import { cwd } from 'vs/base/common/process'; import { IProtocolMainService } from 'vs/platform/protocol/electron-main/protocol'; import { ProtocolMainService } from 'vs/platform/protocol/electron-main/protocolMainService'; import { Promises } from 'vs/base/common/async'; +import { toDisposable } from 'vs/base/common/lifecycle'; /** * The main VS Code entry point. @@ -339,6 +340,9 @@ class CodeMain { throw new ExpectedError('Sent env to running instance. Terminating...'); } + const lockFile = await this.createLockfile(environmentMainService); + once(lifecycleMainService.onWillShutdown)(() => lockFile.dispose()); + // Print --status usage info if (environmentMainService.args.status) { logService.warn('Warning: The --status argument can only be used if Code is already running. Please run it again after Code has started.'); @@ -419,6 +423,18 @@ class CodeMain { lifecycleMainService.kill(exitCode); } + private async createLockfile(env: IEnvironmentMainService) { + await FSPromises.writeFile(env.mainLockfile, String(process.pid)); + + return toDisposable(() => { + try { + unlinkSync(env.mainLockfile); + } catch { + // ignored + } + }); + } + //#region Command line arguments utilities private resolveArgs(): NativeParsedArgs { diff --git a/src/vs/platform/environment/electron-main/environmentMainService.ts b/src/vs/platform/environment/electron-main/environmentMainService.ts index 27c94de78d630..d95965be8639b 100644 --- a/src/vs/platform/environment/electron-main/environmentMainService.ts +++ b/src/vs/platform/environment/electron-main/environmentMainService.ts @@ -30,6 +30,7 @@ export interface IEnvironmentMainService extends INativeEnvironmentService { // --- IPC mainIPCHandle: string; + mainLockfile: string; // --- config sandbox: boolean; @@ -52,6 +53,9 @@ export class EnvironmentMainService extends NativeEnvironmentService implements @memoize get mainIPCHandle(): string { return createStaticIPCHandle(this.userDataPath, 'main', this.productService.version); } + @memoize + get mainLockfile(): string { return join(this.userDataPath, 'code.lock'); } + @memoize get sandbox(): boolean { return !!this.args['__sandbox']; }