-
Notifications
You must be signed in to change notification settings - Fork 85
fix(cli): allow convert to work without a package.json #1009
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
Merged
lorisleiva
merged 6 commits into
codama-idl:main
from
senzenn:fix/convert-without-package-json
Jun 22, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9eefb22
fix(cli): allow convert to work without a package.json
senzenn 70521fa
fix(cli): avoid installing adapters outside projects
senzenn a28690d
Merge branch 'codama-idl:main' into fix/convert-without-package-json
senzenn 4f4d8cb
test(cli): account for colored error output
senzenn de081ad
refactor(cli): simplify missing-module detection
senzenn 1b8667c
refactor(cli): use generic default for npx command args
senzenn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@codama/cli": patch | ||
| --- | ||
|
|
||
| Make `codama convert` work without a `package.json`. The CLI resolves an available `@codama/nodes-from-anchor` adapter directly. If the adapter is missing, it suggests an `npx -p` command instead of creating a `package.json`; projects with a `package.json` keep the existing install prompt. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import type { RootNode } from '@codama/nodes'; | ||
| import pico from 'picocolors'; | ||
| import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { CliError } from '../src/utils/errors'; | ||
| import { importModuleItem } from '../src/utils/import'; | ||
| import { getRootNodeFromIdl } from '../src/utils/nodes'; | ||
| import { installMissingDependencies } from '../src/utils/packageInstall'; | ||
| import { tryGetPackageJson } from '../src/utils/packageJson'; | ||
|
|
||
| vi.mock('../src/utils/import', () => ({ importModuleItem: vi.fn() })); | ||
| vi.mock('../src/utils/packageInstall', () => ({ installMissingDependencies: vi.fn() })); | ||
| vi.mock('../src/utils/packageJson', () => ({ tryGetPackageJson: vi.fn() })); | ||
|
|
||
| const importModuleItemMock = vi.mocked(importModuleItem); | ||
| const installMissingDependenciesMock = vi.mocked(installMissingDependencies); | ||
| const tryGetPackageJsonMock = vi.mocked(tryGetPackageJson); | ||
| const anchorIdl = { instructions: [], metadata: { spec: '0.1.0' } }; | ||
| const rootNode = { kind: 'rootNode', standard: 'codama' } as RootNode; | ||
|
|
||
| describe('getRootNodeFromIdl', () => { | ||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| test('uses an already resolvable Anchor adapter without checking package.json', async () => { | ||
| const rootNodeFromAnchor = vi.fn().mockReturnValue(rootNode); | ||
| importModuleItemMock.mockResolvedValue(rootNodeFromAnchor); | ||
|
|
||
| await expect(getRootNodeFromIdl(anchorIdl)).resolves.toBe(rootNode); | ||
| expect(tryGetPackageJsonMock).not.toHaveBeenCalled(); | ||
| expect(installMissingDependenciesMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('suggests npx without installing when package.json and the adapter are missing', async () => { | ||
| importModuleItemMock.mockRejectedValue(createMissingModuleError('@codama/nodes-from-anchor')); | ||
| tryGetPackageJsonMock.mockResolvedValue(undefined); | ||
|
|
||
| const error = await getRootNodeFromIdl(anchorIdl, { | ||
| npxCommandArgs: ['convert', 'anchor.json', 'codama.json'], | ||
| }).catch((cause: unknown) => cause); | ||
|
|
||
| expect(error).toBeInstanceOf(CliError); | ||
| expect((error as CliError).message).toBe('Anchor IDL support is not available.'); | ||
| expect((error as CliError).items).toEqual([ | ||
| `${pico.bold('Missing dependency')}: @codama/nodes-from-anchor`, | ||
| 'No package.json was found, so Codama did not install dependencies in this directory.', | ||
| `${pico.bold('Re-run with')}: ${pico.yellow( | ||
| 'npx -p codama -p @codama/nodes-from-anchor codama convert anchor.json codama.json', | ||
| )}`, | ||
| ]); | ||
| expect(installMissingDependenciesMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('keeps the install flow when package.json exists', async () => { | ||
| const rootNodeFromAnchor = vi.fn().mockReturnValue(rootNode); | ||
| importModuleItemMock | ||
| .mockRejectedValueOnce(createMissingModuleError('@codama/nodes-from-anchor')) | ||
| .mockResolvedValueOnce(rootNodeFromAnchor); | ||
| tryGetPackageJsonMock.mockResolvedValue({ name: 'example' }); | ||
| installMissingDependenciesMock.mockResolvedValue(true); | ||
|
|
||
| await expect(getRootNodeFromIdl(anchorIdl)).resolves.toBe(rootNode); | ||
| expect(installMissingDependenciesMock).toHaveBeenCalledWith( | ||
| 'Anchor IDL detected. Additional dependencies are required to process Anchor IDLs.', | ||
| ['@codama/nodes-from-anchor'], | ||
| ); | ||
| }); | ||
|
|
||
| test('surfaces adapter load failures instead of treating them as a missing adapter', async () => { | ||
| const loadError = new SyntaxError('Unexpected token'); | ||
| importModuleItemMock.mockRejectedValue(loadError); | ||
|
|
||
| await expect(getRootNodeFromIdl(anchorIdl)).rejects.toBe(loadError); | ||
| expect(tryGetPackageJsonMock).not.toHaveBeenCalled(); | ||
| expect(installMissingDependenciesMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('treats any missing-module error as a missing adapter', async () => { | ||
| importModuleItemMock.mockRejectedValue(createMissingModuleError('missing-transitive-package')); | ||
| tryGetPackageJsonMock.mockResolvedValue(undefined); | ||
|
|
||
| const error = await getRootNodeFromIdl(anchorIdl).catch((cause: unknown) => cause); | ||
|
|
||
| expect(error).toBeInstanceOf(CliError); | ||
| expect((error as CliError).message).toBe('Anchor IDL support is not available.'); | ||
| expect(installMissingDependenciesMock).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| function createMissingModuleError(moduleName: string): CliError { | ||
| const cause = Object.assign(new Error(`Cannot find package '${moduleName}'`), { code: 'ERR_MODULE_NOT_FOUND' }); | ||
| return new CliError('Failed to load module.', [], { cause }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { beforeEach, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { canRead, resolveRelativePath } from '../src/utils/fs'; | ||
|
|
||
| vi.mock('../src/utils/fs', () => ({ | ||
| canRead: vi.fn(), | ||
| readJson: vi.fn(), | ||
| resolveRelativePath: vi.fn(), | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| vi.mocked(canRead).mockReset(); | ||
| vi.mocked(resolveRelativePath).mockReturnValue('/tmp/codama-no-package-json/package.json'); | ||
| }); | ||
|
|
||
| test('returns undefined and no dependencies when package.json is missing', async () => { | ||
| vi.mocked(canRead).mockResolvedValue(false); | ||
| const { getPackageJsonDependencies, tryGetPackageJson } = await import('../src/utils/packageJson'); | ||
|
|
||
| await expect(tryGetPackageJson()).resolves.toBeUndefined(); | ||
| await expect(getPackageJsonDependencies()).resolves.toEqual([]); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.