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
19 changes: 18 additions & 1 deletion packages/cli/src/__tests__/resolve-lint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';

import { describe, expect, it } from '@voidzero-dev/vite-plus-test';

import { lint } from '../resolve-lint.js';
import { lint, resolveWindowsTsgolintExecutable } from '../resolve-lint.js';

describe('resolve-lint', () => {
it('should resolve binPath and OXLINT_TSGOLINT_PATH to existing files', async () => {
Expand All @@ -21,4 +21,21 @@ describe('resolve-lint', () => {
`OXLINT_TSGOLINT_PATH should point to an existing file, got: ${tsgolintPath}`,
).toBe(true);
});

it('should keep the Windows tsgolint executable path absolute', () => {
const tsgolintPath =
'C:\\repo\\node_modules\\.pnpm\\vite-plus@0.1.24\\node_modules\\vite-plus\\node_modules\\.bin\\tsgolint.cmd';
const result = resolveWindowsTsgolintExecutable(
[
'C:\\repo\\node_modules\\.pnpm\\vite-plus@0.1.24\\node_modules\\vite-plus\\node_modules\\.bin\\tsgolint.exe',
tsgolintPath,
],
{
exists: (path) => path === tsgolintPath,
},
);

expect(result).toBe(tsgolintPath);
expect(result).not.toMatch(/^\.\\/);
});
});
57 changes: 35 additions & 22 deletions packages/cli/src/resolve-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,34 @@

import { existsSync, realpathSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { relative } from 'node:path/win32';
import { fileURLToPath } from 'node:url';

import { DEFAULT_ENVS, resolve } from './utils/constants.ts';

export function resolveWindowsTsgolintExecutable(
pathCandidates: string[],
options: {
exists: (path: string) => boolean;
getRealpathCandidates?: () => string[];
},
): string {
let oxlintTsgolintPath = pathCandidates.find((p) => options.exists(p)) ?? '';
if (!oxlintTsgolintPath && options.getRealpathCandidates) {
try {
oxlintTsgolintPath = options.getRealpathCandidates().find((p) => options.exists(p)) ?? '';
} catch {
// realpath failed, fall through to default
}
}
if (!oxlintTsgolintPath) {
throw new Error(
'Unable to resolve oxlint-tsgolint executable, tried:\n' +
pathCandidates.map((path) => `- ${path}`).join('\n'),
);
}
return oxlintTsgolintPath;
}

/**
* Resolves the oxlint binary path and environment variables.
*
Expand Down Expand Up @@ -53,29 +76,19 @@ export async function lint(): Promise<{
join(projectBinDir, 'tsgolint.exe'),
join(projectBinDir, 'tsgolint.cmd'),
];
oxlintTsgolintPath = pathCandidates.find((p) => existsSync(p)) ?? '';
// Bun stores packages in .bun/ cache dirs where the symlinked paths above won't match.
if (!oxlintTsgolintPath) {
try {
oxlintTsgolintPath = resolveWindowsTsgolintExecutable(pathCandidates, {
exists: existsSync,
// Bun stores packages in .bun/ cache dirs where the symlinked paths above won't match.
getRealpathCandidates: () => {
const realPkgDir = realpathSync(join(scriptDir, '..'));
const realBinDir = join(dirname(realPkgDir), '.bin');
oxlintTsgolintPath =
[join(realBinDir, 'tsgolint.exe'), join(realBinDir, 'tsgolint.cmd')].find((p) =>
existsSync(p),
) ?? '';
} catch {
// realpath failed, fall through to default
}
}
if (!oxlintTsgolintPath) {
throw new Error(
'Unable to resolve oxlint-tsgolint executable, tried:\n' +
pathCandidates.map((path) => `- ${path}`).join('\n'),
);
}
const relativePath = relative(process.cwd(), oxlintTsgolintPath);
// Only prepend .\ if it's actually a relative path (not an absolute path returned by relative())
oxlintTsgolintPath = /^[a-zA-Z]:/.test(relativePath) ? relativePath : `.\\${relativePath}`;
return [join(realBinDir, 'tsgolint.exe'), join(realBinDir, 'tsgolint.cmd')];
},
});
// Keep the resolved absolute path. oxlint may be spawned with a different cwd than
// this launcher (e.g. the workspace package dir under `vp run -r`), where a path made
// relative to the launcher's process.cwd() would resolve against the wrong base
// directory and fail (e.g. pnpm's `.pnpm` only exists at the monorepo root).
}
const result = {
binPath,
Expand Down
Loading