Skip to content
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

feat: Support custom toml file path #19

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions 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 @@ -65,6 +66,7 @@ export async function getWorkspaceSettings(
workspace: workspace.uri.toString(),
interpreter: resolveVariables(interpreter, workspace),
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: 24,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'll tweak this before merging.

patch: 0,
};


Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't seem we enforce any type of formatting? Mind if I do a quick follow-up adding Biome?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that works for me 👍 I've typically used prettier but Biome looks good.


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