Unverified Commit 18b18e22 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

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
parent ee76e305
......@@ -5,6 +5,7 @@ const fs = require('fs')
const path = require('path')
const os = require('os')
const { runAgentsMd } = require('../../bin/agents-md')
const { getNextjsVersion } = require('../../lib/agents-md')
/**
* TRUE E2E TESTS
......@@ -291,4 +292,35 @@ This is my project documentation.
process.chdir(originalCwd)
}
}, 30000) // Increase timeout for git clone
describe('getNextjsVersion', () => {
const fixturesDir = path.join(__dirname, 'fixtures/agents-md')
it('returns the installed Next.js version from node_modules', () => {
const fixture = path.join(fixturesDir, 'next-specific-version')
const result = getNextjsVersion(fixture)
expect(result.version).toBe('15.4.0')
expect(result.error).toBeUndefined()
})
it('returns actual installed version, not the tag from package.json', () => {
// package.json has "next": "latest", but node_modules has version "16.0.0"
const fixture = path.join(fixturesDir, 'next-tag')
const result = getNextjsVersion(fixture)
// Should return the actual installed version, not "latest"
expect(result.version).toBe('16.0.0')
expect(result.error).toBeUndefined()
})
it('returns error when Next.js is not installed', () => {
// Use a directory where next is not installed
const nonNextDir = '/tmp'
const result = getNextjsVersion(nonNextDir)
expect(result.version).toBeNull()
expect(result.error).toBe('Next.js is not installed in this project.')
})
})
})
# Allow node_modules in test fixtures
!node_modules/
{
"name": "next-specific-version",
"dependencies": {
"next": "15.4.0"
}
}
{
"name": "next-tag",
"dependencies": {
"next": "latest"
}
}
......@@ -16,27 +16,11 @@ interface NextjsVersionResult {
}
export function getNextjsVersion(cwd: string): NextjsVersionResult {
const packageJsonPath = path.join(cwd, 'package.json')
if (!fs.existsSync(packageJsonPath)) {
return {
version: null,
error: 'No package.json found in the current directory',
}
}
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
const dependencies = packageJson.dependencies || {}
const devDependencies = packageJson.devDependencies || {}
const nextVersion = dependencies.next || devDependencies.next
if (nextVersion) {
const cleanVersion = nextVersion.replace(/^[\^~>=<]+/, '')
return { version: cleanVersion }
}
const nextPkgPath = require.resolve('next/package.json', { paths: [cwd] })
const pkg = JSON.parse(fs.readFileSync(nextPkgPath, 'utf-8'))
return { version: pkg.version }
} catch {
// Not found at root - check for monorepo workspace
const workspace = detectWorkspace(cwd)
if (workspace.isMonorepo && workspace.packages.length > 0) {
......@@ -56,11 +40,6 @@ export function getNextjsVersion(cwd: string): NextjsVersionResult {
version: null,
error: 'Next.js is not installed in this project.',
}
} catch (err) {
return {
version: null,
error: `Failed to parse package.json: ${err instanceof Error ? err.message : String(err)}`,
}
}
}
......@@ -525,18 +504,16 @@ function findNextjsInWorkspace(cwd: string, patterns: string[]): string | null {
const versions: string[] = []
for (const pkgPath of packagePaths) {
const packageJsonPath = path.join(pkgPath, 'package.json')
if (!fs.existsSync(packageJsonPath)) continue
try {
const content = fs.readFileSync(packageJsonPath, 'utf-8')
const pkg = JSON.parse(content)
const nextVersion = pkg.dependencies?.next || pkg.devDependencies?.next
if (nextVersion) {
versions.push(nextVersion.replace(/^[\^~>=<]+/, ''))
const nextPkgPath = require.resolve('next/package.json', {
paths: [pkgPath],
})
const pkg = JSON.parse(fs.readFileSync(nextPkgPath, 'utf-8'))
if (pkg.version) {
versions.push(pkg.version)
}
} catch {
// Skip invalid package.json
// Next.js not installed in this package
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment