Skip to content

Commit c55a3d5

Browse files
Kartik Rajwesm
Kartik Raj
authored andcommitted
Add workaround for hatch environment discovery on Windows (microsoft/vscode-python#23129)
Closes microsoft/vscode-python#23121 cc/ @karthiknadig
1 parent d122b31 commit c55a3d5

File tree

1 file changed

+24
-1
lines changed
  • extensions/positron-python/src/client/pythonEnvironments/common/environmentManagers

1 file changed

+24
-1
lines changed

extensions/positron-python/src/client/pythonEnvironments/common/environmentManagers/hatch.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isTestExecution } from '../../../common/constants';
22
import { exec, pathExists } from '../externalDependencies';
33
import { traceVerbose } from '../../../logging';
44
import { cache } from '../../../common/utils/decorators';
5+
import { getOSType, OSType } from '../../../common/utils/platform';
56

67
/** Wraps the "Hatch" utility, and exposes its functionality.
78
*/
@@ -22,7 +23,9 @@ export class Hatch {
2223
* first argument of spawn() - i.e. it can be a full path, or just a binary name.
2324
* @param cwd - The working directory to use as cwd when running hatch.
2425
*/
25-
constructor(public readonly command: string, private cwd: string) {}
26+
constructor(public readonly command: string, private cwd: string) {
27+
this.fixCwd();
28+
}
2629

2730
/**
2831
* Returns a Hatch instance corresponding to the binary which can be used to run commands for the cwd.
@@ -90,4 +93,24 @@ export class Hatch {
9093
);
9194
return envPaths.flatMap((r) => (r ? [r] : []));
9295
}
96+
97+
/**
98+
* Due to an upstream hatch issue on Windows https://github.com/pypa/hatch/issues/1350,
99+
* 'hatch env find default' does not handle case-insensitive paths as cwd, which are valid on Windows.
100+
* So we need to pass the case-exact path as cwd.
101+
* It has been observed that only the drive letter in `cwd` is lowercased here. Unfortunately,
102+
* there's no good way to get case of the drive letter correctly without using Win32 APIs:
103+
* https://stackoverflow.com/questions/33086985/how-to-obtain-case-exact-path-of-a-file-in-node-js-on-windows
104+
* So we do it manually.
105+
*/
106+
private fixCwd(): void {
107+
if (getOSType() === OSType.Windows) {
108+
if (/^[a-z]:/.test(this.cwd)) {
109+
// Replace first character by the upper case version of the character.
110+
const a = this.cwd.split(':');
111+
a[0] = a[0].toUpperCase();
112+
this.cwd = a.join(':');
113+
}
114+
}
115+
}
93116
}

0 commit comments

Comments
 (0)