Skip to content

Fix double semicolon in PATH environment variable on Windows #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/extension/noConfigDebugInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ export async function registerNoConfigDebug(

const noConfigScriptsDir = path.join(extPath, 'bundled', 'scripts', 'noConfigScripts');
const pathSeparator = process.platform === 'win32' ? ';' : ':';
collection.append('PATH', `${pathSeparator}${noConfigScriptsDir}`);

// Check if the current PATH already ends with a path separator to avoid double separators
const currentPath = process.env.PATH || '';
const needsSeparator = currentPath.length > 0 && !currentPath.endsWith(pathSeparator);
const pathValueToAppend = needsSeparator ? `${pathSeparator}${noConfigScriptsDir}` : noConfigScriptsDir;

collection.append('PATH', pathValueToAppend);

const bundledDebugPath = path.join(extPath, 'bundled', 'libs', 'debugpy');
collection.replace('BUNDLED_DEBUGPY_PATH', bundledDebugPath);
Expand Down
96 changes: 95 additions & 1 deletion src/test/unittest/noConfigDebugInit.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ suite('setup for no-config debug scenario', function () {
.setup((x) => x.append(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.callback((key, value) => {
if (key === 'PATH') {
assert(value === `:${noConfigScriptsDir}`);
assert(value.includes(noConfigScriptsDir));
}
})
.returns(envVarCollectionAppendStub);
Expand All @@ -88,6 +88,100 @@ suite('setup for no-config debug scenario', function () {
sinon.assert.calledOnce(envVarCollectionAppendStub);
});

test('should not add extra separator when PATH already ends with separator', async () => {
const environmentVariableCollectionMock = TypeMoq.Mock.ofType<any>();
envVarCollectionReplaceStub = sinon.stub();
envVarCollectionAppendStub = sinon.stub();

// Simulate a PATH that already ends with a separator to test the fix
const pathSeparator = process.platform === 'win32' ? ';' : ':';
const originalPath = process.env.PATH;
process.env.PATH = `/some/path${pathSeparator}`;

try {
environmentVariableCollectionMock
.setup((x) => x.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(envVarCollectionReplaceStub);

environmentVariableCollectionMock
.setup((x) => x.append(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.callback((key, value) => {
if (key === 'PATH') {
// Since PATH already ends with separator, we should NOT add another one
assert(value === noConfigScriptsDir);
assert(!value.startsWith(pathSeparator));
}
})
.returns(envVarCollectionAppendStub);

context
.setup((c) => c.environmentVariableCollection)
.returns(() => environmentVariableCollectionMock.object);

setupFileSystemWatchers();

// run init for no config debug
await registerNoConfigDebug(context.object.environmentVariableCollection, context.object.extensionPath);

// assert that append was called for PATH
sinon.assert.calledOnce(envVarCollectionAppendStub);
} finally {
// Restore original PATH
if (originalPath !== undefined) {
process.env.PATH = originalPath;
} else {
delete process.env.PATH;
}
}
});

test('should add separator when PATH does not end with separator', async () => {
const environmentVariableCollectionMock = TypeMoq.Mock.ofType<any>();
envVarCollectionReplaceStub = sinon.stub();
envVarCollectionAppendStub = sinon.stub();

// Simulate a PATH that does NOT end with a separator
const pathSeparator = process.platform === 'win32' ? ';' : ':';
const originalPath = process.env.PATH;
process.env.PATH = '/some/path';

try {
environmentVariableCollectionMock
.setup((x) => x.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(envVarCollectionReplaceStub);

environmentVariableCollectionMock
.setup((x) => x.append(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.callback((key, value) => {
if (key === 'PATH') {
// Since PATH does NOT end with separator, we should add one
assert(value === `${pathSeparator}${noConfigScriptsDir}`);
assert(value.startsWith(pathSeparator));
}
})
.returns(envVarCollectionAppendStub);

context
.setup((c) => c.environmentVariableCollection)
.returns(() => environmentVariableCollectionMock.object);

setupFileSystemWatchers();

// run init for no config debug
await registerNoConfigDebug(context.object.environmentVariableCollection, context.object.extensionPath);

// assert that append was called for PATH
sinon.assert.calledOnce(envVarCollectionAppendStub);
} finally {
// Restore original PATH
if (originalPath !== undefined) {
process.env.PATH = originalPath;
} else {
delete process.env.PATH;
}
}
});

test('should create file system watcher for debuggerAdapterEndpointFolder', async () => {
// Arrange
const environmentVariableCollectionMock = TypeMoq.Mock.ofType<any>();
Expand Down