Skip to content

Commit

Permalink
Support custom file path
Browse files Browse the repository at this point in the history
  • Loading branch information
max-muoto committed Feb 12, 2025
1 parent 0552980 commit 6aff600
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"configuration": {
"properties": {
"tach.importStrategy": {
"default": "useBundled",
"default": "fromEnvironment",
"description": "Defines where `tach` is imported from. This setting may be ignored if `tach.path` is set.",
"enum": [
"useBundled",
Expand All @@ -90,6 +90,12 @@
"type": "string"
},
"type": "array"
},
"tach.configuration": {
"default": "",
"description": "Path to a `tach.toml` file to use for configuration. By default, the extension will mirror the behavior that the `tach` CLI would have.",
"scope": "resource",
"type": "string"
}
}
},
Expand Down
33 changes: 33 additions & 0 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,30 @@ import { traceError, traceInfo, traceVerbose } from './log/logging';
import { getExtensionSettings, getGlobalSettings, getWorkspaceSettings, ISettings } from './settings';
import { getLSClientTraceLevel, getProjectRoot } from './utilities';
import { isVirtualWorkspace } from './vscodeapi';
import { execFile } from 'child_process';
import { supportsCustomConfig, VersionInfo } from './version';

export type IInitOptions = { settings: ISettings[]; globalSettings: ISettings };

function executeCommand(file: string, args: string[] = []): Promise<string> {
return new Promise((resolve, reject) => {
execFile(file, args, (error, stdout, stderr) => {
if (error) {
reject(new Error(stderr || error.message));
} else {
resolve(stdout);
}
});
});
}

async function getTachVersion(pythonExecutable: string): Promise<VersionInfo> {
const stdout = await executeCommand(pythonExecutable, ["-m", "tach", "--version"]);
const version = stdout.trim().split(" ")[1];
const [major, minor, patch] = version.split(".").map((x) => parseInt(x, 10));
return new VersionInfo(major, minor, patch);
}

async function createServer(
settings: ISettings,
serverId: string,
Expand All @@ -34,7 +55,19 @@ async function createServer(
newEnv.PYTHONPATH = BUNDLED_PYTHON_LIBS_DIR;
}


const args = settings.interpreter.slice(1).concat(["-m", "tach", "server"]);

if (settings.configuration) {
const version = await getTachVersion(command);
if (!supportsCustomConfig(version)) {
traceError(`Server: Tach version ${version.toString()} does not support custom configuration files.`);
} else {
traceInfo(`Server: Using custom configuration file: ${settings.configuration}`);
args.push("-c", settings.configuration);
}
}

traceInfo(`Server run command: ${[command, ...args].join(' ')}`);

const serverOptions: ServerOptions = {
Expand Down
5 changes: 4 additions & 1 deletion src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ISettings {
workspace: string;
interpreter: string[];
importStrategy: ImportStrategy;
configuration: string | null;
}

export function getExtensionSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings[]> {
Expand Down Expand Up @@ -64,7 +65,8 @@ export async function getWorkspaceSettings(
cwd: workspace.uri.fsPath,
workspace: workspace.uri.toString(),
interpreter: resolveVariables(interpreter, workspace),
importStrategy: config.get<ImportStrategy>(`importStrategy`) ?? 'useBundled',
importStrategy: config.get<ImportStrategy>(`importStrategy`) ?? 'fromEnvironment',
configuration: config.get<string>(`configuration`) ?? null,
};
return workspaceSetting;
}
Expand All @@ -90,6 +92,7 @@ export async function getGlobalSettings(namespace: string, includeInterpreter?:
workspace: process.cwd(),
interpreter: interpreter,
importStrategy: getGlobalValue<ImportStrategy>(config, 'importStrategy', 'useBundled'),
configuration: getGlobalValue<string | null>(config, 'configuration', null),
};
return setting;
}
Expand Down
33 changes: 33 additions & 0 deletions src/common/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export class VersionInfo {
constructor(
public major: number,
public minor: number,
public patch: number
) {}

toString(): string {
return `${this.major}.${this.minor}.${this.patch}`;
}
}

function versionGte(a: VersionInfo, b: VersionInfo): boolean {
if (a.major !== b.major) {
return a.major > b.major;
}
if (a.minor !== b.minor) {
return a.minor > b.minor;
}
return a.patch >= b.patch;
}

const MIN_VERSION_WITH_CONFIG = {
major: 0,
minor: 25,
patch: 2,
};



export function supportsCustomConfig(version: VersionInfo): boolean {
return versionGte(version, MIN_VERSION_WITH_CONFIG);
}

0 comments on commit 6aff600

Please sign in to comment.