From 93caf87414ed69a2f17b39584b9755fc9ba3bdef Mon Sep 17 00:00:00 2001 From: Peaupoosh <45771097+Pduhard@users.noreply.github.com> Date: Wed, 14 Oct 2026 18:57:28 +0200 Subject: [PATCH 1/2] fix(cli): run the bundled oxlint and oxfmt unless the project declares them vp lint and vp fmt resolved oxlint, oxlint-tsgolint and oxfmt from process.cwd() first, and Node walks up to the filesystem root. A project that does not install these tools itself picked up any copy in a parent directory, such as the main checkout around a git worktree. That copy can be an older oxlint that predates Vite+ mode: it walks nested configs past the project and applies the parent's .oxlintrc.json, so every lint.overrides entry in vite.config.ts is ignored. The project's copy now runs only when the nearest package.json or the workspace root declares it, and it is installed between that package and the workspace root, the rule resolve-core already applies to Vite. Otherwise the copy shipped with the CLI runs. Yarn PnP resolves the declared copy through its own resolver. findWorkspaceRoot moves to utils/workspace-root.ts, next to findPackageRoot and dirsUpTo, so the lint and fmt resolvers do not load Vite. Co-Authored-By: Claude Opus 5.5 --- .../cli/src/__tests__/resolve-fmt.spec.ts | 62 +++++++ .../cli/src/__tests__/resolve-lint.spec.ts | 61 ++++++- packages/cli/src/resolve-fmt.ts | 5 +- packages/cli/src/resolve-lint.ts | 5 +- packages/cli/src/resolve-vite-config.ts | 3 + .../__tests__/resolve-project-tool.spec.ts | 151 ++++++++++++++++++ .../cli/src/utils/resolve-project-tool.ts | 67 ++++++++ packages/cli/src/utils/workspace-root.ts | 53 ++++++ 8 files changed, 401 insertions(+), 6 deletions(-) create mode 100644 packages/cli/src/__tests__/resolve-fmt.spec.ts create mode 100644 packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts create mode 100644 packages/cli/src/utils/resolve-project-tool.ts create mode 100644 packages/cli/src/utils/workspace-root.ts diff --git a/packages/cli/src/__tests__/resolve-fmt.spec.ts b/packages/cli/src/__tests__/resolve-fmt.spec.ts new file mode 100644 index 0000000000..34a3d83036 --- /dev/null +++ b/packages/cli/src/__tests__/resolve-fmt.spec.ts @@ -0,0 +1,62 @@ +import { existsSync, 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 { fmt } from '../resolve-fmt.js'; + +function writeFakeOxfmt(nodeModulesDir: string) { + const pkgDir = join(nodeModulesDir, 'oxfmt'); + mkdirSync(join(pkgDir, 'dist'), { recursive: true }); + writeFileSync( + join(pkgDir, 'package.json'), + JSON.stringify({ name: 'oxfmt', version: '0.0.0-fake', main: 'dist/index.js' }), + ); + writeFileSync(join(pkgDir, 'dist', 'index.js'), ''); + return pkgDir; +} + +function writePackageJson(dir: string, pkg: Record) { + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); +} + +describe('resolve-fmt', () => { + let originalCwd: string; + let tmp: string; + + beforeEach(() => { + originalCwd = process.cwd(); + tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-resolve-fmt-'))); + }); + + afterEach(() => { + process.chdir(originalCwd); + rmSync(tmp, { recursive: true, force: true }); + }); + + it('runs the bundled oxfmt, not one installed in a parent directory', async () => { + writePackageJson(tmp, { name: 'parent', devDependencies: { oxfmt: '0.45.0' } }); + writeFakeOxfmt(join(tmp, 'node_modules')); + const project = join(tmp, 'project'); + writePackageJson(project, { name: 'app', devDependencies: { 'vite-plus': '1.2.0' } }); + process.chdir(project); + + const { binPath } = await fmt(); + + expect(binPath.startsWith(tmp)).toBe(false); + expect(existsSync(binPath)).toBe(true); + }); + + it('runs the oxfmt the project declares', async () => { + const project = join(tmp, 'project'); + writePackageJson(project, { name: 'app', devDependencies: { oxfmt: '0.45.0' } }); + const fakeOxfmt = writeFakeOxfmt(join(project, 'node_modules')); + process.chdir(project); + + const { binPath } = await fmt(); + + expect(binPath).toBe(join(fakeOxfmt, 'bin', 'oxfmt')); + }); +}); diff --git a/packages/cli/src/__tests__/resolve-lint.spec.ts b/packages/cli/src/__tests__/resolve-lint.spec.ts index ae2ff1e279..322a265899 100644 --- a/packages/cli/src/__tests__/resolve-lint.spec.ts +++ b/packages/cli/src/__tests__/resolve-lint.spec.ts @@ -1,10 +1,67 @@ -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 { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { lint, resolveWindowsLintBackendExecutable } from '../resolve-lint.ts'; +function writeFakeOxlint(nodeModulesDir: string) { + const pkgDir = join(nodeModulesDir, 'oxlint'); + mkdirSync(join(pkgDir, 'dist'), { recursive: true }); + writeFileSync( + join(pkgDir, 'package.json'), + JSON.stringify({ name: 'oxlint', version: '0.0.0-fake', main: 'dist/index.js' }), + ); + writeFileSync(join(pkgDir, 'dist', 'index.js'), ''); + return pkgDir; +} + +function writePackageJson(dir: string, pkg: Record) { + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); +} + describe('resolve-lint', () => { + describe('which oxlint runs', () => { + let originalCwd: string; + let tmp: string; + + beforeEach(() => { + originalCwd = process.cwd(); + tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-resolve-lint-'))); + }); + + afterEach(() => { + process.chdir(originalCwd); + rmSync(tmp, { recursive: true, force: true }); + }); + + it('runs the bundled oxlint, not one installed in a parent directory such as the main checkout around a git worktree', async () => { + writePackageJson(tmp, { name: 'parent', devDependencies: { oxlint: '1.60.0' } }); + writeFakeOxlint(join(tmp, 'node_modules')); + const project = join(tmp, 'project'); + writePackageJson(project, { name: 'app', devDependencies: { 'vite-plus': '1.2.0' } }); + process.chdir(project); + + const { binPath } = await lint(); + + expect(binPath.startsWith(tmp)).toBe(false); + expect(existsSync(binPath)).toBe(true); + }); + + it('runs the oxlint the project declares', async () => { + const project = join(tmp, 'project'); + writePackageJson(project, { name: 'app', devDependencies: { oxlint: '1.60.0' } }); + const fakeOxlint = writeFakeOxlint(join(project, 'node_modules')); + process.chdir(project); + + const { binPath } = await lint(); + + expect(binPath).toBe(join(fakeOxlint, 'bin', 'oxlint')); + }); + }); + 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/resolve-vite-config.ts b/packages/cli/src/resolve-vite-config.ts index c10186907f..9152891c00 100644 --- a/packages/cli/src/resolve-vite-config.ts +++ b/packages/cli/src/resolve-vite-config.ts @@ -4,6 +4,9 @@ import path from 'node:path'; import { findMonorepoRoot } from '../binding/index.js'; import { withConfigMetadataResolution } from './define-config.ts'; import { VITE_CONFIG_FILES } from './utils/constants.ts'; +import { findWorkspaceRoot } from './utils/workspace-root.ts'; + +export { findWorkspaceRoot }; /** * Find a vite config file by walking up from `startDir` to `stopDir`. 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..f92c343eb6 --- /dev/null +++ b/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts @@ -0,0 +1,151 @@ +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import path, { join } from 'node:path'; + +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; + +import { resolveBundled } from '../constants.js'; +import { resolveProjectTool } from '../resolve-project-tool.js'; +import { dirsUpTo, findPackageRoot } from '../workspace-root.js'; + +function writeFakeOxlint(nodeModulesDir: string) { + const pkgDir = join(nodeModulesDir, 'oxlint'); + mkdirSync(join(pkgDir, 'dist'), { recursive: true }); + writeFileSync( + join(pkgDir, 'package.json'), + JSON.stringify({ name: 'oxlint', version: '0.0.0-fake', main: 'dist/index.js' }), + ); + writeFileSync(join(pkgDir, 'dist', 'index.js'), ''); + return join(pkgDir, 'dist', 'index.js'); +} + +function writePackageJson(dir: string, pkg: Record) { + mkdirSync(dir, { recursive: true }); + writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); +} + +const declaresOxlint = { devDependencies: { oxlint: '1.60.0' } }; + +describe('resolveProjectTool', () => { + let tmp: string; + let bundled: string; + + beforeEach(() => { + tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-project-tool-'))); + bundled = resolveBundled('oxlint'); + }); + + afterEach(() => { + rmSync(tmp, { recursive: true, force: true }); + delete process.versions.pnp; + }); + + it('uses the copy declared and installed at the pnpm workspace root from a workspace package', () => { + writePackageJson(tmp, { name: 'root', ...declaresOxlint }); + writeFileSync(join(tmp, 'pnpm-workspace.yaml'), 'packages:\n - packages/*\n'); + const rootOxlint = writeFakeOxlint(join(tmp, 'node_modules')); + const pkg = join(tmp, 'packages', 'a'); + writePackageJson(pkg, { name: 'a' }); + + expect(resolveProjectTool('oxlint', pkg)).toBe(rootOxlint); + }); + + it('finds a copy that npm hoisted to the workspace root for a package that declares it', () => { + writePackageJson(tmp, { name: 'root', workspaces: ['packages/*'] }); + const rootOxlint = writeFakeOxlint(join(tmp, 'node_modules')); + const pkg = join(tmp, 'packages', 'a'); + writePackageJson(pkg, { name: 'a', ...declaresOxlint }); + + expect(resolveProjectTool('oxlint', pkg)).toBe(rootOxlint); + }); + + it('ignores a copy installed in the project that the project does not declare', () => { + writePackageJson(tmp, { name: 'app' }); + writeFakeOxlint(join(tmp, 'node_modules')); + + expect(resolveProjectTool('oxlint', tmp)).toBe(bundled); + }); + + it('ignores a copy declared by a package.json above the nearest package when no workspace links them', () => { + writePackageJson(tmp, { name: 'outer', ...declaresOxlint }); + writeFakeOxlint(join(tmp, 'node_modules')); + const pkg = join(tmp, 'packages', 'a'); + writePackageJson(pkg, { name: 'a' }); + + expect(resolveProjectTool('oxlint', pkg)).toBe(bundled); + }); + + it('ignores a declared copy that is only installed above the workspace root', () => { + writePackageJson(tmp, { name: 'outer', ...declaresOxlint }); + writeFakeOxlint(join(tmp, 'node_modules')); + const project = join(tmp, 'project'); + writePackageJson(project, { name: 'app', ...declaresOxlint }); + + expect(resolveProjectTool('oxlint', project)).toBe(bundled); + }); + + it('treats a package under a pnpm workspace root as part of that workspace, like vp run', () => { + writePackageJson(tmp, { name: 'root', ...declaresOxlint }); + writeFileSync(join(tmp, 'pnpm-workspace.yaml'), 'packages:\n - packages/*\n'); + const rootOxlint = writeFakeOxlint(join(tmp, 'node_modules')); + const standalone = join(tmp, 'tools', 'standalone'); + writePackageJson(standalone, { name: 'standalone' }); + + expect(resolveProjectTool('oxlint', standalone)).toBe(rootOxlint); + }); + + it('uses the bundled copy outside any package', () => { + const loose = join(tmp, 'loose'); + mkdirSync(loose); + writeFakeOxlint(join(tmp, 'node_modules')); + + expect(findPackageRoot(loose)).toBeUndefined(); + expect(resolveProjectTool('oxlint', loose)).toBe(bundled); + }); + + it('keeps the declaration check under Yarn PnP', () => { + process.versions.pnp = '3'; + const project = join(tmp, 'project'); + writePackageJson(project, { name: 'app', ...declaresOxlint }); + const projectOxlint = writeFakeOxlint(join(project, 'node_modules')); + const other = join(tmp, 'other'); + writePackageJson(other, { name: 'other' }); + + expect(resolveProjectTool('oxlint', project)).toBe(projectOxlint); + expect(resolveProjectTool('oxlint', other)).toBe(bundled); + }); +}); + +describe('dirsUpTo', () => { + it('stops at the given ancestor', () => { + expect([...dirsUpTo('C:\\repo\\packages\\a', 'C:\\repo', path.win32)]).toEqual([ + 'C:\\repo\\packages\\a', + 'C:\\repo\\packages', + 'C:\\repo', + ]); + }); + + it('stops at a Windows drive root', () => { + expect([...dirsUpTo('C:\\repo\\a', undefined, path.win32)]).toEqual([ + 'C:\\repo\\a', + 'C:\\repo', + 'C:\\', + ]); + }); + + it('stops at a Windows UNC share root', () => { + expect([...dirsUpTo('\\\\server\\share\\repo', undefined, path.win32)]).toEqual([ + '\\\\server\\share\\repo', + '\\\\server\\share\\', + ]); + }); + + it('stops at the root when the ancestor is not on the path', () => { + expect([...dirsUpTo('C:\\repo\\a', 'D:\\other', path.win32)]).toEqual([ + 'C:\\repo\\a', + 'C:\\repo', + 'C:\\', + ]); + expect([...dirsUpTo('/repo/a', '/other', path.posix)]).toEqual(['/repo/a', '/repo', '/']); + }); +}); 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..da873f48d6 --- /dev/null +++ b/packages/cli/src/utils/resolve-project-tool.ts @@ -0,0 +1,67 @@ +import fs from 'node:fs'; +import { createRequire } from 'node:module'; +import path from 'node:path'; + +import { resolveBundled } from './constants.ts'; +import { dirsUpTo, findPackageRoot, findWorkspaceRoot } from './workspace-root.ts'; + +const require = createRequire(import.meta.url); + +const DEPENDENCY_FIELDS = [ + 'dependencies', + 'devDependencies', + 'optionalDependencies', + 'peerDependencies', +] as const; + +function packageNameOf(specifier: string): string { + const segments = specifier.split('/'); + return specifier.startsWith('@') ? segments.slice(0, 2).join('/') : segments[0]; +} + +function declaresDependency(dir: string, packageName: string): boolean { + try { + const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')) as Record< + string, + Record | undefined + >; + return DEPENDENCY_FIELDS.some((field) => Object.hasOwn(pkg?.[field] ?? {}, packageName)); + } catch { + return false; + } +} + +function resolveInstalled( + specifier: string, + packageName: string, + owner: string, + workspaceRoot: string, +): string | undefined { + if (process.versions.pnp) { + try { + return require.resolve(specifier, { paths: [owner] }); + } catch { + return undefined; + } + } + for (const dir of dirsUpTo(owner, workspaceRoot)) { + if (fs.existsSync(path.join(dir, 'node_modules', packageName))) { + return require.resolve(specifier, { paths: [dir] }); + } + } + return undefined; +} + +export function resolveProjectTool(specifier: string, cwd = process.cwd()): string { + const packageName = packageNameOf(specifier); + const packageRoot = findPackageRoot(cwd); + if (packageRoot) { + const workspaceRoot = findWorkspaceRoot(packageRoot) ?? packageRoot; + const owner = [packageRoot, workspaceRoot].find((dir) => declaresDependency(dir, packageName)); + const resolved = owner && resolveInstalled(specifier, packageName, owner, workspaceRoot); + if (resolved) { + return resolved; + } + } + return resolveBundled(specifier); +} diff --git a/packages/cli/src/utils/workspace-root.ts b/packages/cli/src/utils/workspace-root.ts new file mode 100644 index 0000000000..67660272ad --- /dev/null +++ b/packages/cli/src/utils/workspace-root.ts @@ -0,0 +1,53 @@ +import fs from 'node:fs'; +import path from 'node:path'; + +/** + * Find the workspace root by walking up from `startDir` looking for + * monorepo indicators (pnpm-workspace.yaml, workspaces in package.json, lerna.json). + */ +export function findWorkspaceRoot(startDir: string): string | undefined { + let dir = path.resolve(startDir); + while (true) { + if (fs.existsSync(path.join(dir, 'pnpm-workspace.yaml'))) { + return dir; + } + const pkgPath = path.join(dir, 'package.json'); + if (fs.existsSync(pkgPath)) { + try { + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); + if (pkg.workspaces) { + return dir; + } + } catch { + // Skip malformed package.json and continue searching parent directories + } + } + if (fs.existsSync(path.join(dir, 'lerna.json'))) { + return dir; + } + const parent = path.dirname(dir); + if (parent === dir) { + break; + } + dir = parent; + } + return undefined; +} + +export function* dirsUpTo(from: string, to?: string, pathImpl: path.PlatformPath = path) { + for (let dir = from; ; dir = pathImpl.dirname(dir)) { + yield dir; + if (dir === to || dir === pathImpl.dirname(dir)) { + return; + } + } +} + +export function findPackageRoot(startDir: string): string | undefined { + for (const dir of dirsUpTo(path.resolve(startDir))) { + if (fs.existsSync(path.join(dir, 'package.json'))) { + return dir; + } + } + return undefined; +} From a013b2e8cbda485bd2fad8b4e4ef4aafee03ed1b Mon Sep 17 00:00:00 2001 From: Peaupoosh <45771097+Pduhard@users.noreply.github.com> Date: Thu, 15 Oct 2026 12:04:03 +0200 Subject: [PATCH 2/2] refactor(cli): resolve declared tools the way resolve-core does Read the nearest package.json with readNearestPackageJson and resolve with createRequire, as resolve-core does for Vite, which also covers Yarn PnP. Find the workspace root with findMonorepoRoot, as resolve-vite-config does. Drop dirsUpTo, findPackageRoot, the PnP branch and the installed-between check; keep one regression test through lint() plus the rule's own spec. Co-Authored-By: Claude Opus 5.5 --- .../cli/src/__tests__/resolve-fmt.spec.ts | 62 -------- .../cli/src/__tests__/resolve-lint.spec.ts | 66 ++------- packages/cli/src/resolve-vite-config.ts | 3 - .../__tests__/resolve-project-tool.spec.ts | 138 ++++-------------- .../cli/src/utils/resolve-project-tool.ts | 71 +++------ packages/cli/src/utils/workspace-root.ts | 53 ------- 6 files changed, 58 insertions(+), 335 deletions(-) delete mode 100644 packages/cli/src/__tests__/resolve-fmt.spec.ts delete mode 100644 packages/cli/src/utils/workspace-root.ts diff --git a/packages/cli/src/__tests__/resolve-fmt.spec.ts b/packages/cli/src/__tests__/resolve-fmt.spec.ts deleted file mode 100644 index 34a3d83036..0000000000 --- a/packages/cli/src/__tests__/resolve-fmt.spec.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { existsSync, 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 { fmt } from '../resolve-fmt.js'; - -function writeFakeOxfmt(nodeModulesDir: string) { - const pkgDir = join(nodeModulesDir, 'oxfmt'); - mkdirSync(join(pkgDir, 'dist'), { recursive: true }); - writeFileSync( - join(pkgDir, 'package.json'), - JSON.stringify({ name: 'oxfmt', version: '0.0.0-fake', main: 'dist/index.js' }), - ); - writeFileSync(join(pkgDir, 'dist', 'index.js'), ''); - return pkgDir; -} - -function writePackageJson(dir: string, pkg: Record) { - mkdirSync(dir, { recursive: true }); - writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); -} - -describe('resolve-fmt', () => { - let originalCwd: string; - let tmp: string; - - beforeEach(() => { - originalCwd = process.cwd(); - tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-resolve-fmt-'))); - }); - - afterEach(() => { - process.chdir(originalCwd); - rmSync(tmp, { recursive: true, force: true }); - }); - - it('runs the bundled oxfmt, not one installed in a parent directory', async () => { - writePackageJson(tmp, { name: 'parent', devDependencies: { oxfmt: '0.45.0' } }); - writeFakeOxfmt(join(tmp, 'node_modules')); - const project = join(tmp, 'project'); - writePackageJson(project, { name: 'app', devDependencies: { 'vite-plus': '1.2.0' } }); - process.chdir(project); - - const { binPath } = await fmt(); - - expect(binPath.startsWith(tmp)).toBe(false); - expect(existsSync(binPath)).toBe(true); - }); - - it('runs the oxfmt the project declares', async () => { - const project = join(tmp, 'project'); - writePackageJson(project, { name: 'app', devDependencies: { oxfmt: '0.45.0' } }); - const fakeOxfmt = writeFakeOxfmt(join(project, 'node_modules')); - process.chdir(project); - - const { binPath } = await fmt(); - - expect(binPath).toBe(join(fakeOxfmt, 'bin', 'oxfmt')); - }); -}); diff --git a/packages/cli/src/__tests__/resolve-lint.spec.ts b/packages/cli/src/__tests__/resolve-lint.spec.ts index 322a265899..5ec20307b3 100644 --- a/packages/cli/src/__tests__/resolve-lint.spec.ts +++ b/packages/cli/src/__tests__/resolve-lint.spec.ts @@ -2,64 +2,30 @@ import { existsSync, mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync import { tmpdir } from 'node:os'; import { join } from 'node:path'; -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { lint, resolveWindowsLintBackendExecutable } from '../resolve-lint.ts'; -function writeFakeOxlint(nodeModulesDir: string) { - const pkgDir = join(nodeModulesDir, 'oxlint'); - mkdirSync(join(pkgDir, 'dist'), { recursive: true }); - writeFileSync( - join(pkgDir, 'package.json'), - JSON.stringify({ name: 'oxlint', version: '0.0.0-fake', main: 'dist/index.js' }), - ); - writeFileSync(join(pkgDir, 'dist', 'index.js'), ''); - return pkgDir; -} - -function writePackageJson(dir: string, pkg: Record) { - mkdirSync(dir, { recursive: true }); - writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); -} - describe('resolve-lint', () => { - describe('which oxlint runs', () => { - let originalCwd: string; - let tmp: string; - - beforeEach(() => { - originalCwd = process.cwd(); - tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-resolve-lint-'))); - }); - - afterEach(() => { - process.chdir(originalCwd); - rmSync(tmp, { recursive: true, force: true }); - }); - - it('runs the bundled oxlint, not one installed in a parent directory such as the main checkout around a git worktree', async () => { - writePackageJson(tmp, { name: 'parent', devDependencies: { oxlint: '1.60.0' } }); - writeFakeOxlint(join(tmp, 'node_modules')); - const project = join(tmp, 'project'); - writePackageJson(project, { name: 'app', devDependencies: { 'vite-plus': '1.2.0' } }); - process.chdir(project); + 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); - expect(existsSync(binPath)).toBe(true); - }); - - it('runs the oxlint the project declares', async () => { - const project = join(tmp, 'project'); - writePackageJson(project, { name: 'app', devDependencies: { oxlint: '1.60.0' } }); - const fakeOxlint = writeFakeOxlint(join(project, 'node_modules')); - process.chdir(project); - - const { binPath } = await lint(); - - expect(binPath).toBe(join(fakeOxlint, 'bin', 'oxlint')); - }); + } finally { + process.chdir(originalCwd); + rmSync(tmp, { recursive: true, force: true }); + } }); it('should resolve binPath and OXLINT_TSGOLINT_PATH to existing files', async () => { diff --git a/packages/cli/src/resolve-vite-config.ts b/packages/cli/src/resolve-vite-config.ts index 9152891c00..c10186907f 100644 --- a/packages/cli/src/resolve-vite-config.ts +++ b/packages/cli/src/resolve-vite-config.ts @@ -4,9 +4,6 @@ import path from 'node:path'; import { findMonorepoRoot } from '../binding/index.js'; import { withConfigMetadataResolution } from './define-config.ts'; import { VITE_CONFIG_FILES } from './utils/constants.ts'; -import { findWorkspaceRoot } from './utils/workspace-root.ts'; - -export { findWorkspaceRoot }; /** * Find a vite config file by walking up from `startDir` to `stopDir`. diff --git a/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts b/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts index f92c343eb6..635c30c509 100644 --- a/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts +++ b/packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts @@ -1,151 +1,63 @@ import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; -import path, { join } from 'node:path'; +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'; -import { dirsUpTo, findPackageRoot } from '../workspace-root.js'; - -function writeFakeOxlint(nodeModulesDir: string) { - const pkgDir = join(nodeModulesDir, 'oxlint'); - mkdirSync(join(pkgDir, 'dist'), { recursive: true }); - writeFileSync( - join(pkgDir, 'package.json'), - JSON.stringify({ name: 'oxlint', version: '0.0.0-fake', main: 'dist/index.js' }), - ); - writeFileSync(join(pkgDir, 'dist', 'index.js'), ''); - return join(pkgDir, 'dist', 'index.js'); -} function writePackageJson(dir: string, pkg: Record) { mkdirSync(dir, { recursive: true }); writeFileSync(join(dir, 'package.json'), JSON.stringify(pkg)); } -const declaresOxlint = { devDependencies: { oxlint: '1.60.0' } }; +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; - let bundled: string; beforeEach(() => { tmp = realpathSync(mkdtempSync(join(tmpdir(), 'vp-project-tool-'))); - bundled = resolveBundled('oxlint'); }); afterEach(() => { rmSync(tmp, { recursive: true, force: true }); - delete process.versions.pnp; - }); - - it('uses the copy declared and installed at the pnpm workspace root from a workspace package', () => { - writePackageJson(tmp, { name: 'root', ...declaresOxlint }); - writeFileSync(join(tmp, 'pnpm-workspace.yaml'), 'packages:\n - packages/*\n'); - const rootOxlint = writeFakeOxlint(join(tmp, 'node_modules')); - const pkg = join(tmp, 'packages', 'a'); - writePackageJson(pkg, { name: 'a' }); - - expect(resolveProjectTool('oxlint', pkg)).toBe(rootOxlint); }); - it('finds a copy that npm hoisted to the workspace root for a package that declares it', () => { - writePackageJson(tmp, { name: 'root', workspaces: ['packages/*'] }); - const rootOxlint = writeFakeOxlint(join(tmp, 'node_modules')); - const pkg = join(tmp, 'packages', 'a'); - writePackageJson(pkg, { name: 'a', ...declaresOxlint }); + 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', pkg)).toBe(rootOxlint); + expect(resolveProjectTool('oxlint', join(tmp, 'project'))).toBe(resolveBundled('oxlint')); }); - it('ignores a copy installed in the project that the project does not declare', () => { - writePackageJson(tmp, { name: 'app' }); - writeFakeOxlint(join(tmp, 'node_modules')); + it('should ignore an oxlint the project installs but does not declare', () => { + writePackageJson(tmp, {}); + installOxlint(tmp); - expect(resolveProjectTool('oxlint', tmp)).toBe(bundled); + expect(resolveProjectTool('oxlint', tmp)).toBe(resolveBundled('oxlint')); }); - it('ignores a copy declared by a package.json above the nearest package when no workspace links them', () => { - writePackageJson(tmp, { name: 'outer', ...declaresOxlint }); - writeFakeOxlint(join(tmp, 'node_modules')); - const pkg = join(tmp, 'packages', 'a'); - writePackageJson(pkg, { name: 'a' }); + it('should use the oxlint the project declares', () => { + writePackageJson(tmp, { devDependencies: { oxlint: '1.60.0' } }); + const oxlint = installOxlint(tmp); - expect(resolveProjectTool('oxlint', pkg)).toBe(bundled); + expect(resolveProjectTool('oxlint', tmp)).toBe(oxlint); }); - it('ignores a declared copy that is only installed above the workspace root', () => { - writePackageJson(tmp, { name: 'outer', ...declaresOxlint }); - writeFakeOxlint(join(tmp, 'node_modules')); - const project = join(tmp, 'project'); - writePackageJson(project, { name: 'app', ...declaresOxlint }); - - expect(resolveProjectTool('oxlint', project)).toBe(bundled); - }); - - it('treats a package under a pnpm workspace root as part of that workspace, like vp run', () => { - writePackageJson(tmp, { name: 'root', ...declaresOxlint }); + 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'); - const rootOxlint = writeFakeOxlint(join(tmp, 'node_modules')); - const standalone = join(tmp, 'tools', 'standalone'); - writePackageJson(standalone, { name: 'standalone' }); - - expect(resolveProjectTool('oxlint', standalone)).toBe(rootOxlint); - }); - - it('uses the bundled copy outside any package', () => { - const loose = join(tmp, 'loose'); - mkdirSync(loose); - writeFakeOxlint(join(tmp, 'node_modules')); - - expect(findPackageRoot(loose)).toBeUndefined(); - expect(resolveProjectTool('oxlint', loose)).toBe(bundled); - }); - - it('keeps the declaration check under Yarn PnP', () => { - process.versions.pnp = '3'; - const project = join(tmp, 'project'); - writePackageJson(project, { name: 'app', ...declaresOxlint }); - const projectOxlint = writeFakeOxlint(join(project, 'node_modules')); - const other = join(tmp, 'other'); - writePackageJson(other, { name: 'other' }); - - expect(resolveProjectTool('oxlint', project)).toBe(projectOxlint); - expect(resolveProjectTool('oxlint', other)).toBe(bundled); - }); -}); - -describe('dirsUpTo', () => { - it('stops at the given ancestor', () => { - expect([...dirsUpTo('C:\\repo\\packages\\a', 'C:\\repo', path.win32)]).toEqual([ - 'C:\\repo\\packages\\a', - 'C:\\repo\\packages', - 'C:\\repo', - ]); - }); - - it('stops at a Windows drive root', () => { - expect([...dirsUpTo('C:\\repo\\a', undefined, path.win32)]).toEqual([ - 'C:\\repo\\a', - 'C:\\repo', - 'C:\\', - ]); - }); - - it('stops at a Windows UNC share root', () => { - expect([...dirsUpTo('\\\\server\\share\\repo', undefined, path.win32)]).toEqual([ - '\\\\server\\share\\repo', - '\\\\server\\share\\', - ]); - }); + writePackageJson(join(tmp, 'packages', 'a'), {}); + const oxlint = installOxlint(tmp); - it('stops at the root when the ancestor is not on the path', () => { - expect([...dirsUpTo('C:\\repo\\a', 'D:\\other', path.win32)]).toEqual([ - 'C:\\repo\\a', - 'C:\\repo', - 'C:\\', - ]); - expect([...dirsUpTo('/repo/a', '/other', path.posix)]).toEqual(['/repo/a', '/repo', '/']); + 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 index da873f48d6..0d271d011c 100644 --- a/packages/cli/src/utils/resolve-project-tool.ts +++ b/packages/cli/src/utils/resolve-project-tool.ts @@ -1,66 +1,29 @@ -import fs from 'node:fs'; import { createRequire } from 'node:module'; import path from 'node:path'; +import { findMonorepoRoot } from '../../binding/index.js'; import { resolveBundled } from './constants.ts'; -import { dirsUpTo, findPackageRoot, findWorkspaceRoot } from './workspace-root.ts'; - -const require = createRequire(import.meta.url); - -const DEPENDENCY_FIELDS = [ - 'dependencies', - 'devDependencies', - 'optionalDependencies', - 'peerDependencies', -] as const; - -function packageNameOf(specifier: string): string { - const segments = specifier.split('/'); - return specifier.startsWith('@') ? segments.slice(0, 2).join('/') : segments[0]; -} +import { readNearestPackageJson } from './package.ts'; function declaresDependency(dir: string, packageName: string): boolean { - try { - const pkg = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')) as Record< - string, - Record | undefined - >; - return DEPENDENCY_FIELDS.some((field) => Object.hasOwn(pkg?.[field] ?? {}, packageName)); - } catch { - return false; - } -} - -function resolveInstalled( - specifier: string, - packageName: string, - owner: string, - workspaceRoot: string, -): string | undefined { - if (process.versions.pnp) { - try { - return require.resolve(specifier, { paths: [owner] }); - } catch { - return undefined; - } - } - for (const dir of dirsUpTo(owner, workspaceRoot)) { - if (fs.existsSync(path.join(dir, 'node_modules', packageName))) { - return require.resolve(specifier, { paths: [dir] }); - } - } - return undefined; + 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 = packageNameOf(specifier); - const packageRoot = findPackageRoot(cwd); - if (packageRoot) { - const workspaceRoot = findWorkspaceRoot(packageRoot) ?? packageRoot; - const owner = [packageRoot, workspaceRoot].find((dir) => declaresDependency(dir, packageName)); - const resolved = owner && resolveInstalled(specifier, packageName, owner, workspaceRoot); - if (resolved) { - return resolved; + 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); diff --git a/packages/cli/src/utils/workspace-root.ts b/packages/cli/src/utils/workspace-root.ts deleted file mode 100644 index 67660272ad..0000000000 --- a/packages/cli/src/utils/workspace-root.ts +++ /dev/null @@ -1,53 +0,0 @@ -import fs from 'node:fs'; -import path from 'node:path'; - -/** - * Find the workspace root by walking up from `startDir` looking for - * monorepo indicators (pnpm-workspace.yaml, workspaces in package.json, lerna.json). - */ -export function findWorkspaceRoot(startDir: string): string | undefined { - let dir = path.resolve(startDir); - while (true) { - if (fs.existsSync(path.join(dir, 'pnpm-workspace.yaml'))) { - return dir; - } - const pkgPath = path.join(dir, 'package.json'); - if (fs.existsSync(pkgPath)) { - try { - const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')); - if (pkg.workspaces) { - return dir; - } - } catch { - // Skip malformed package.json and continue searching parent directories - } - } - if (fs.existsSync(path.join(dir, 'lerna.json'))) { - return dir; - } - const parent = path.dirname(dir); - if (parent === dir) { - break; - } - dir = parent; - } - return undefined; -} - -export function* dirsUpTo(from: string, to?: string, pathImpl: path.PlatformPath = path) { - for (let dir = from; ; dir = pathImpl.dirname(dir)) { - yield dir; - if (dir === to || dir === pathImpl.dirname(dir)) { - return; - } - } -} - -export function findPackageRoot(startDir: string): string | undefined { - for (const dir of dirsUpTo(path.resolve(startDir))) { - if (fs.existsSync(path.join(dir, 'package.json'))) { - return dir; - } - } - return undefined; -}