Skip to content
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
25 changes: 24 additions & 1 deletion packages/cli/src/__tests__/resolve-lint.spec.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/resolve-fmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/resolve-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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(
Expand Down
63 changes: 63 additions & 0 deletions packages/cli/src/utils/__tests__/resolve-project-tool.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) {
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);
});
});
30 changes: 30 additions & 0 deletions packages/cli/src/utils/resolve-project-tool.ts
Original file line number Diff line number Diff line change
@@ -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);
}