Skip to content

Commit 18b18e2

Browse files
authored
fix(agents-md): use require.resolve() to get the installed next version (#89166)
### Why? When the package.json has Next.js version with a tag instead of a specific version, e.g., `"next": "canary"`, it received an error: ``` Failed to pull docs: Could not find documentation for Next.js vcanary. This version may not exist on GitHub yet. ``` ### How? Modified logic to get version to use `require.resolve()`. #### Before https://github.com/user-attachments/assets/487d0404-ad81-4c5c-a341-cab9a0e03020 #### After https://github.com/user-attachments/assets/4153873f-681f-4c46-8ee0-ee7913749b7b
1 parent ee76e30 commit 18b18e2

File tree

7 files changed

+65
-34
lines changed

7 files changed

+65
-34
lines changed

packages/next-codemod/lib/__tests__/agents-md-e2e.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fs = require('fs')
55
const path = require('path')
66
const os = require('os')
77
const { runAgentsMd } = require('../../bin/agents-md')
8+
const { getNextjsVersion } = require('../../lib/agents-md')
89

910
/**
1011
* TRUE E2E TESTS
@@ -291,4 +292,35 @@ This is my project documentation.
291292
process.chdir(originalCwd)
292293
}
293294
}, 30000) // Increase timeout for git clone
295+
296+
describe('getNextjsVersion', () => {
297+
const fixturesDir = path.join(__dirname, 'fixtures/agents-md')
298+
299+
it('returns the installed Next.js version from node_modules', () => {
300+
const fixture = path.join(fixturesDir, 'next-specific-version')
301+
const result = getNextjsVersion(fixture)
302+
303+
expect(result.version).toBe('15.4.0')
304+
expect(result.error).toBeUndefined()
305+
})
306+
307+
it('returns actual installed version, not the tag from package.json', () => {
308+
// package.json has "next": "latest", but node_modules has version "16.0.0"
309+
const fixture = path.join(fixturesDir, 'next-tag')
310+
const result = getNextjsVersion(fixture)
311+
312+
// Should return the actual installed version, not "latest"
313+
expect(result.version).toBe('16.0.0')
314+
expect(result.error).toBeUndefined()
315+
})
316+
317+
it('returns error when Next.js is not installed', () => {
318+
// Use a directory where next is not installed
319+
const nonNextDir = '/tmp'
320+
const result = getNextjsVersion(nonNextDir)
321+
322+
expect(result.version).toBeNull()
323+
expect(result.error).toBe('Next.js is not installed in this project.')
324+
})
325+
})
294326
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Allow node_modules in test fixtures
2+
!node_modules/

packages/next-codemod/lib/__tests__/fixtures/agents-md/next-specific-version/node_modules/next/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "next-specific-version",
3+
"dependencies": {
4+
"next": "15.4.0"
5+
}
6+
}

packages/next-codemod/lib/__tests__/fixtures/agents-md/next-tag/node_modules/next/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "next-tag",
3+
"dependencies": {
4+
"next": "latest"
5+
}
6+
}

packages/next-codemod/lib/agents-md.ts

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,11 @@ interface NextjsVersionResult {
1616
}
1717

1818
export function getNextjsVersion(cwd: string): NextjsVersionResult {
19-
const packageJsonPath = path.join(cwd, 'package.json')
20-
21-
if (!fs.existsSync(packageJsonPath)) {
22-
return {
23-
version: null,
24-
error: 'No package.json found in the current directory',
25-
}
26-
}
27-
2819
try {
29-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
30-
const dependencies = packageJson.dependencies || {}
31-
const devDependencies = packageJson.devDependencies || {}
32-
33-
const nextVersion = dependencies.next || devDependencies.next
34-
35-
if (nextVersion) {
36-
const cleanVersion = nextVersion.replace(/^[\^~>=<]+/, '')
37-
return { version: cleanVersion }
38-
}
39-
20+
const nextPkgPath = require.resolve('next/package.json', { paths: [cwd] })
21+
const pkg = JSON.parse(fs.readFileSync(nextPkgPath, 'utf-8'))
22+
return { version: pkg.version }
23+
} catch {
4024
// Not found at root - check for monorepo workspace
4125
const workspace = detectWorkspace(cwd)
4226
if (workspace.isMonorepo && workspace.packages.length > 0) {
@@ -56,11 +40,6 @@ export function getNextjsVersion(cwd: string): NextjsVersionResult {
5640
version: null,
5741
error: 'Next.js is not installed in this project.',
5842
}
59-
} catch (err) {
60-
return {
61-
version: null,
62-
error: `Failed to parse package.json: ${err instanceof Error ? err.message : String(err)}`,
63-
}
6443
}
6544
}
6645

@@ -525,18 +504,16 @@ function findNextjsInWorkspace(cwd: string, patterns: string[]): string | null {
525504
const versions: string[] = []
526505

527506
for (const pkgPath of packagePaths) {
528-
const packageJsonPath = path.join(pkgPath, 'package.json')
529-
if (!fs.existsSync(packageJsonPath)) continue
530-
531507
try {
532-
const content = fs.readFileSync(packageJsonPath, 'utf-8')
533-
const pkg = JSON.parse(content)
534-
const nextVersion = pkg.dependencies?.next || pkg.devDependencies?.next
535-
if (nextVersion) {
536-
versions.push(nextVersion.replace(/^[\^~>=<]+/, ''))
508+
const nextPkgPath = require.resolve('next/package.json', {
509+
paths: [pkgPath],
510+
})
511+
const pkg = JSON.parse(fs.readFileSync(nextPkgPath, 'utf-8'))
512+
if (pkg.version) {
513+
versions.push(pkg.version)
537514
}
538515
} catch {
539-
// Skip invalid package.json
516+
// Next.js not installed in this package
540517
}
541518
}
542519

0 commit comments

Comments
 (0)