From 3fbc5d33a0014a671840f63f2281a434e646d43e Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Mon, 25 Mar 2024 20:50:40 -0700 Subject: [PATCH] Fix hatch environment discovery on Windows --- .../common/environmentManagers/hatch.ts | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/client/pythonEnvironments/common/environmentManagers/hatch.ts b/src/client/pythonEnvironments/common/environmentManagers/hatch.ts index a22ce38bbffb..6d7a13ea1557 100644 --- a/src/client/pythonEnvironments/common/environmentManagers/hatch.ts +++ b/src/client/pythonEnvironments/common/environmentManagers/hatch.ts @@ -2,6 +2,7 @@ import { isTestExecution } from '../../../common/constants'; import { exec, pathExists } from '../externalDependencies'; import { traceVerbose } from '../../../logging'; import { cache } from '../../../common/utils/decorators'; +import { getOSType, OSType } from '../../../common/utils/platform'; /** Wraps the "Hatch" utility, and exposes its functionality. */ @@ -22,7 +23,9 @@ export class Hatch { * first argument of spawn() - i.e. it can be a full path, or just a binary name. * @param cwd - The working directory to use as cwd when running hatch. */ - constructor(public readonly command: string, private cwd: string) {} + constructor(public readonly command: string, private cwd: string) { + this.fixCwd(); + } /** * 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 { ); return envPaths.flatMap((r) => (r ? [r] : [])); } + + /** + * Due to an upstream hatch issue on Windows https://github.com/pypa/hatch/issues/1350, + * 'hatch env find default' does not handle case-insensitive paths as cwd, which are valid on Windows. + * So we need to pass the case-exact path as cwd. + * It has been observed that only the drive letter in `cwd` is lowercased here. Unfortunately, + * there's no good way to get case of the drive letter correctly without using Win32 APIs: + * https://stackoverflow.com/questions/33086985/how-to-obtain-case-exact-path-of-a-file-in-node-js-on-windows + * So we do it manually. + */ + private fixCwd(): void { + if (getOSType() === OSType.Windows) { + if (/^[a-z]:/.test(this.cwd)) { + // Replace first character by the upper case version of the character. + const a = this.cwd.split(':'); + a[0] = a[0].toUpperCase(); + this.cwd = a.join(':'); + } + } + } }