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(nx): allow text lockfile for bun #29423

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions e2e/utils/get-env-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export function getPublishedVersion(): string {
}

export function detectPackageManager(dir: string = ''): PackageManager {
return existsSync(join(dir, 'bun.lockb'))
? 'bun'
: existsSync(join(dir, 'yarn.lock'))
return existsSync(join(dir, 'yarn.lock'))
? 'yarn'
Copy link
Author

Choose a reason for hiding this comment

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

made yarn the first lockfile just to make the bun/pnpm indentation identical for multiple lockfiles

Copy link
Contributor

Choose a reason for hiding this comment

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

e bun/pnpm indentation identical for multiple lockfiles

Doesnt bun current support to yarn lock still, This is why bun came frist

Copy link
Author

Choose a reason for hiding this comment

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

right, great point, will reverse

: existsSync(join(dir, 'bun.lockb')) ||
existsSync(join(dir, 'bun.lock'))
? 'bun'
: existsSync(join(dir, 'pnpm-lock.yaml')) ||
existsSync(join(dir, 'pnpm-workspace.yaml'))
? 'pnpm'
Expand Down
7 changes: 4 additions & 3 deletions packages/create-nx-workspace/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ export const packageManagerList = ['pnpm', 'yarn', 'npm', 'bun'] as const;
export type PackageManager = (typeof packageManagerList)[number];

export function detectPackageManager(dir: string = ''): PackageManager {
return existsSync(join(dir, 'bun.lockb'))
? 'bun'
: existsSync(join(dir, 'yarn.lock'))
return existsSync(join(dir, 'yarn.lock'))
? 'yarn'
: existsSync(join(dir, 'pnpm-lock.yaml'))
? 'pnpm'
: existsSync(join(dir, 'bun.lockb')) ||
existsSync(join(dir, 'bun.lock'))
? 'bun'
: 'npm';
}

Expand Down
1 change: 1 addition & 0 deletions packages/devkit/src/utils/replace-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function replaceMentions(
'package-lock.json',
'pnpm-lock.yaml',
'bun.lockb',
'bun.lock',
'CHANGELOG.md',
];
if (ignoredFiles.includes(basename(path))) {
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/daemon/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ function lockFileHashChanged(): boolean {
join(workspaceRoot, 'yarn.lock'),
join(workspaceRoot, 'pnpm-lock.yaml'),
join(workspaceRoot, 'bun.lockb'),
join(workspaceRoot, 'bun.lock'),
]
.filter((file) => existsSync(file))
.map((file) => hashFile(file));
Expand Down
7 changes: 5 additions & 2 deletions packages/nx/src/plugins/js/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ const YARN_LOCK_FILE = 'yarn.lock';
const NPM_LOCK_FILE = 'package-lock.json';
const PNPM_LOCK_FILE = 'pnpm-lock.yaml';
const BUN_LOCK_FILE = 'bun.lockb';
const BUN_TEXT_LOCK_FILE = 'bun.lock';
export const LOCKFILES = [
YARN_LOCK_FILE,
NPM_LOCK_FILE,
PNPM_LOCK_FILE,
BUN_LOCK_FILE,
BUN_TEXT_LOCK_FILE,
];

const YARN_LOCK_PATH = join(workspaceRoot, YARN_LOCK_FILE);
const NPM_LOCK_PATH = join(workspaceRoot, NPM_LOCK_FILE);
const PNPM_LOCK_PATH = join(workspaceRoot, PNPM_LOCK_FILE);
const BUN_LOCK_PATH = join(workspaceRoot, BUN_LOCK_FILE);
const BUN_TEXT_LOCK_PATH = join(workspaceRoot, BUN_TEXT_LOCK_FILE);

/**
* Parses lock file and maps dependencies and metadata to {@link LockFileGraph}
Expand Down Expand Up @@ -143,7 +146,7 @@ export function lockFileExists(packageManager: PackageManager): boolean {
return existsSync(NPM_LOCK_PATH);
}
if (packageManager === 'bun') {
return existsSync(BUN_LOCK_PATH);
return existsSync(BUN_LOCK_PATH) || existsSync(BUN_TEXT_LOCK_PATH);
}
throw new Error(
`Unknown package manager ${packageManager} or lock file missing`
Expand Down Expand Up @@ -182,7 +185,7 @@ function getLockFilePath(packageManager: PackageManager): string {
return NPM_LOCK_PATH;
}
if (packageManager === 'bun') {
return BUN_LOCK_PATH;
return BUN_LOCK_PATH; // defaulting to binary lockfile
}
throw new Error(`Unknown package manager: ${packageManager}`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('getTouchedProjectsFromLockFile', () => {
'pnpm-lock.yaml',
'pnpm-lock.yml',
'bun.lockb',
'bun.lock',
].forEach((lockFile) => {
describe(`"${lockFile}"`, () => {
it(`should not return changes when "${lockFile}" is not touched`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getTouchedProjectsFromLockFile: TouchedProjectLocator<
'pnpm-lock.yaml',
'pnpm-lock.yml',
'bun.lockb',
'bun.lock',
];

if (fileChanges.some((f) => lockFiles.includes(f.file))) {
Expand Down
25 changes: 25 additions & 0 deletions packages/nx/src/utils/package-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('package-manager', () => {
return false;
case 'bun.lockb':
return false;
case 'bun.lock':
return false;
default:
return jest.requireActual('fs').existsSync(p);
}
Expand All @@ -66,6 +68,8 @@ describe('package-manager', () => {
return false;
case 'bun.lockb':
return false;
case 'bun.lock':
return false;
default:
return jest.requireActual('fs').existsSync(p);
}
Expand Down Expand Up @@ -96,6 +100,27 @@ describe('package-manager', () => {
expect(fs.existsSync).toHaveBeenCalledTimes(1);
});

it('should detect bun package manager from bun.lock', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
jest.spyOn(fs, 'existsSync').mockImplementation((p) => {
switch (p) {
case 'yarn.lock':
return false;
case 'pnpm-lock.yaml':
return false;
case 'package-lock.json':
return false;
case 'bun.lock':
return true;
default:
return jest.requireActual('fs').existsSync(p);
}
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('bun');
expect(fs.existsSync).toHaveBeenCalledTimes(1);
});

it('should use npm package manager as default', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
jest.spyOn(fs, 'existsSync').mockImplementation((p) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export function detectPackageManager(dir: string = ''): PackageManager {
const nxJson = readNxJson();
return (
nxJson.cli?.packageManager ??
(existsSync(join(dir, 'bun.lockb'))
existsSync(join(dir, 'bun.lockb')) ||
existsSync(join(dir, 'bun.lock'))
? 'bun'
: existsSync(join(dir, 'yarn.lock'))
? 'yarn'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ jest.mock('fs', () => {
existsSync: (p) =>
p.endsWith('yarn.lock') ||
p.endsWith('pnpm-lock.yaml') ||
p.endsWith('bun.lockb')
p.endsWith('bun.lockb') ||
p.endsWith('bun.lock')
? memFs.existsSync(p)
: actualFs.existsSync(p),
};
Expand Down
5 changes: 5 additions & 0 deletions scripts/check-lock-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function checkLockFiles() {
'Invalid occurence of "bun.lockb" file. Please remove it and use only "pnpm-lock.yaml"'
);
}
if (fs.existsSync('bun.lock')) {
errors.push(
'Invalid occurence of "bun.lockb" file. Please remove it and use only "pnpm-lock.yaml"'
);
}
if (fs.existsSync('yarn.lock')) {
errors.push(
'Invalid occurence of "yarn.lock" file. Please remove it and use only "pnpm-lock.yaml"'
Expand Down