diff --git a/packages/cli/src/__tests__/resolve-lint.spec.ts b/packages/cli/src/__tests__/resolve-lint.spec.ts index ae2ff1e279..5ec20307b3 100644 --- a/packages/cli/src/__tests__/resolve-lint.spec.ts +++ b/packages/cli/src/__tests__/resolve-lint.spec.ts @@ -1,10 +1,33 @@ -import { existsSync } from 'node:fs'; +import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; import { describe, expect, it, vi } from 'vitest'; import { lint, resolveWindowsLintBackendExecutable } from '../resolve-lint.ts'; describe('resolve-lint', () => { + it('should not run an oxlint installed in a parent directory', async () => { + const originalCwd = process.cwd(); + const tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-resolve-lint-'))); + try { + writeFileSync(join(tmp, 'package.json'), '{"devDependencies":{"oxlint":"1.60.0"}}'); + mkdirSync(join(tmp, 'node_modules', 'oxlint'), { recursive: true }); + writeFileSync(join(tmp, 'node_modules', 'oxlint', 'package.json'), '{"name":"oxlint"}'); + writeFileSync(join(tmp, 'node_modules', 'oxlint', 'index.js'), ''); + mkdirSync(join(tmp, 'project')); + writeFileSync(join(tmp, 'project', 'package.json'), '{}'); + process.chdir(join(tmp, 'project')); + + const { binPath } = await lint(); + + expect(binPath.startsWith(tmp)).toBe(false); + } finally { + process.chdir(originalCwd); + rmSync(tmp, { recursive: true, force: true }); + } + }); + it('should resolve binPath and OXLINT_TSGOLINT_PATH to existing files', async () => { const result = await lint(); diff --git a/packages/cli/src/resolve-fmt.ts b/packages/cli/src/resolve-fmt.ts index 9c2c2e2cc6..fbad43dc72 100644 --- a/packages/cli/src/resolve-fmt.ts +++ b/packages/cli/src/resolve-fmt.ts @@ -13,7 +13,8 @@ import { dirname, join } from 'node:path'; -import { CONFIG_METADATA_ENV, DEFAULT_ENVS, resolve } from './utils/constants.ts'; +import { CONFIG_METADATA_ENV, DEFAULT_ENVS } from './utils/constants.ts'; +import { resolveProjectTool } from './utils/resolve-project-tool.ts'; /** * Resolves the oxfmt binary path and environment variables. @@ -34,7 +35,7 @@ export async function fmt(): Promise<{ // resolve the main entry point and derive the bin path from it. // resolve('oxfmt') returns .../oxfmt/dist/index.js, so we need to go up // two directories (past 'dist') to reach the package root. - const oxfmtMainPath = resolve('oxfmt'); + const oxfmtMainPath = resolveProjectTool('oxfmt'); const binPath = join(dirname(dirname(oxfmtMainPath)), 'bin', 'oxfmt'); return { diff --git a/packages/cli/src/resolve-lint.ts b/packages/cli/src/resolve-lint.ts index 20cbf871bc..3d92a5a110 100644 --- a/packages/cli/src/resolve-lint.ts +++ b/packages/cli/src/resolve-lint.ts @@ -13,8 +13,9 @@ import { dirname, join } from 'node:path'; -import { CONFIG_METADATA_ENV, DEFAULT_ENVS, resolve, resolveBundled } from './utils/constants.ts'; +import { CONFIG_METADATA_ENV, DEFAULT_ENVS, resolveBundled } from './utils/constants.ts'; import { resolveLintBackendExecutable } from './utils/lint-backend-path.ts'; +import { resolveProjectTool } from './utils/resolve-project-tool.ts'; export { resolveWindowsLintBackendExecutable } from './utils/lint-backend-path.ts'; @@ -37,7 +38,7 @@ export async function lint(): Promise<{ // resolve the main entry point and derive the bin path from it. // resolve('oxlint') returns .../oxlint/dist/index.js, so we need to go up // two directories (past 'dist') to reach the package root. - const oxlintMainPath = resolve('oxlint'); + const oxlintMainPath = resolveProjectTool('oxlint'); const oxlintPackageRoot = dirname(dirname(oxlintMainPath)); const binPath = join(oxlintPackageRoot, 'bin', 'oxlint'); const tsrsLintPath = resolveLintBackendExecutable( diff --git a/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts b/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts new file mode 100644 index 0000000000..635c30c509 --- /dev/null +++ b/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts @@ -0,0 +1,63 @@ +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; + +import { resolveBundled } from '../constants.js'; +import { resolveProjectTool } from '../resolve-project-tool.js'; + +function writePackageJson(dir: string, pkg: Record) { + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); +} + +function installOxlint(dir: string) { + const pkgDir = join(dir, 'node_modules', 'oxlint'); + writePackageJson(pkgDir, { name: 'oxlint' }); + writeFileSync(join(pkgDir, 'index.js'), ''); + return join(pkgDir, 'index.js'); +} + +describe('resolveProjectTool', () => { + let tmp: string; + + beforeEach(() => { + tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-project-tool-'))); + }); + + afterEach(() => { + rmSync(tmp, { recursive: true, force: true }); + }); + + it('should ignore an oxlint installed in a parent directory', () => { + writePackageJson(tmp, { devDependencies: { oxlint: '1.60.0' } }); + installOxlint(tmp); + writePackageJson(join(tmp, 'project'), {}); + + expect(resolveProjectTool('oxlint', join(tmp, 'project'))).toBe(resolveBundled('oxlint')); + }); + + it('should ignore an oxlint the project installs but does not declare', () => { + writePackageJson(tmp, {}); + installOxlint(tmp); + + expect(resolveProjectTool('oxlint', tmp)).toBe(resolveBundled('oxlint')); + }); + + it('should use the oxlint the project declares', () => { + writePackageJson(tmp, { devDependencies: { oxlint: '1.60.0' } }); + const oxlint = installOxlint(tmp); + + expect(resolveProjectTool('oxlint', tmp)).toBe(oxlint); + }); + + it('should use the oxlint the workspace root declares from a workspace package', () => { + writePackageJson(tmp, { devDependencies: { oxlint: '1.60.0' } }); + writeFileSync(join(tmp, 'pnpm-workspace.yaml'), 'packages:\n - packages/*\n'); + writePackageJson(join(tmp, 'packages', 'a'), {}); + const oxlint = installOxlint(tmp); + + expect(resolveProjectTool('oxlint', join(tmp, 'packages', 'a'))).toBe(oxlint); + }); +}); diff --git a/packages/cli/src/utils/resolve-project-tool.ts b/packages/cli/src/utils/resolve-project-tool.ts new file mode 100644 index 0000000000..0d271d011c --- /dev/null +++ b/packages/cli/src/utils/resolve-project-tool.ts @@ -0,0 +1,30 @@ +import { createRequire } from 'node:module'; +import path from 'node:path'; + +import { findMonorepoRoot } from '../../binding/index.js'; +import { resolveBundled } from './constants.ts'; +import { readNearestPackageJson } from './package.ts'; + +function declaresDependency(dir: string, packageName: string): boolean { + const pkg = readNearestPackageJson(dir); + return ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies'].some( + (field) => Object.hasOwn(pkg?.[field] ?? {}, packageName), + ); +} + +export function resolveProjectTool(specifier: string, cwd = process.cwd()): string { + const packageName = specifier.split('/')[0]; + const owner = [cwd, findMonorepoRoot(cwd)].find( + (dir) => dir && declaresDependency(dir, packageName), + ); + if (owner) { + try { + return createRequire(path.join(owner, 'package.json')).resolve(specifier); + } catch (cause) { + if ((cause as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') { + throw cause; + } + } + } + return resolveBundled(specifier); +}