Skip to content
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
14 changes: 6 additions & 8 deletions src/extension/debugger/adapter/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { sendTelemetryEvent } from '../../telemetry';
import { Commands, EXTENSION_ROOT_DIR } from '../../common/constants';
import { Common, DebugConfigStrings, Interpreters } from '../../common/utils/localize';
import { IPersistentStateFactory } from '../../common/types';
import { fileToCommandArgumentForPythonExt } from '../../common/stringUtils';
import { PythonEnvironment } from '../../envExtApi';
import { resolveEnvironment, getInterpreterDetails, runPythonExtensionCommand } from '../../common/python';

Expand Down Expand Up @@ -79,13 +78,12 @@ export class DebugAdapterDescriptorFactory implements IDebugAdapterDescriptorFac
}

let executable = command.shift() ?? 'python';

// Always ensure interpreter/command is quoted if necessary. Previously this was
// only done in the debugAdapterPath branch which meant that in the common case
// (using the built‑in adapter path) an interpreter path containing spaces would
// be passed unquoted, resulting in a fork/spawn failure on Windows. See bug
// report for details.
executable = fileToCommandArgumentForPythonExt(executable);
// DO NOT apply shell-style quoting here.
// The 'executable' path is passed to 'DebugAdapterExecutable', which internally
// uses 'child_process.spawn' in a non-shell environment.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this breaking the original fix in a shell environment? Seems like fileToCommandArgumentForPythonExt should have some way of checking how the launch is going to happen.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this breaking the original fix in a shell environment? Seems like fileToCommandArgumentForPythonExt should have some way of checking how the launch is going to happen.

Yep, totally agreed — simply removing fileToCommandArgumentForPythonExt is not sufficient.
The correct fix should depend on whether spawn is running in a shell environment:

  • If options.shell === true, we need quotes to handle spaces correctly.
  • If options.shell === false (default), we must avoid quotes to prevent ENOENT.

So instead of removing the helper entirely, we should update fileToCommandArgumentForPythonExt to accept a isShell flag and return the path accordingly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's a shell environment. It's just whether or not this command is a bat/cmd file.
https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/debug/node/debugAdapter.ts#L173-L180

So maybe this is the correct fix.

// Manual quoting will cause the OS (especially Windows) to treat the quotes
// as part of the filename, leading to ENOENT.
// See regression reported in #1013 and analysis of #964.

// "logToFile" is not handled directly by the adapter - instead, we need to pass
// the corresponding CLI switch when spawning it.
Expand Down
12 changes: 4 additions & 8 deletions src/test/unittest/adapter/factory.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,28 +304,24 @@ suite('Debugging - Adapter Factory', () => {

assert.deepStrictEqual(descriptor, debugExecutable);
});
test('Add quotes to interpreter path with spaces (default adapter path)', async () => {
test('Should not add quotes to interpreter path with spaces (default adapter path)', async () => {
const session = createSession({});
const interpreterPathSpaces = 'path/to/python interpreter with spaces';
const interpreterPathSpacesQuoted = `"${interpreterPathSpaces}"`;
const debugExecutable = new DebugAdapterExecutable(interpreterPathSpacesQuoted, [debugAdapterPath]);

const debugExecutable = new DebugAdapterExecutable(interpreterPathSpaces, [debugAdapterPath]);
getInterpreterDetailsStub.resolves({ path: [interpreterPathSpaces] });
const interpreterSpacePath: PythonEnvironment = createInterpreter(interpreterPathSpaces, '3.7.4-test');
// Add architecture for completeness.
(interpreterSpacePath as any).architecture = Architecture.Unknown;
resolveEnvironmentStub.withArgs(interpreterPathSpaces).resolves(interpreterSpacePath);
const descriptor = await factory.createDebugAdapterDescriptor(session, nodeExecutable);

assert.deepStrictEqual(descriptor, debugExecutable);
});

test('Add quotes to interpreter path with spaces when debugAdapterPath is specified', async () => {
test('Should not add quotes to interpreter path with spaces when debugAdapterPath is specified', async () => {
const customAdapterPath = 'custom/debug/adapter/customAdapterPath';
const session = createSession({ debugAdapterPath: customAdapterPath });
const interpreterPathSpaces = 'path/to/python interpreter with spaces';
const interpreterPathSpacesQuoted = `"${interpreterPathSpaces}"`;
const debugExecutable = new DebugAdapterExecutable(interpreterPathSpacesQuoted, [customAdapterPath]);
const debugExecutable = new DebugAdapterExecutable(interpreterPathSpaces, [customAdapterPath]);

getInterpreterDetailsStub.resolves({ path: [interpreterPathSpaces] });
const interpreterSpacePath: PythonEnvironment = createInterpreter(interpreterPathSpaces, '3.7.4-test');
Expand Down
Loading