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

Resolve to real path before checking for path inequality #17279

Merged
merged 7 commits into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -98,6 +98,7 @@
"npm-run-all": "4.1.5",
"nprogress": "0.2.0",
"pixrem": "5.0.0",
"pnpm": "5.8.0",
"postcss-nested": "4.2.1",
"postcss-pseudoelements": "5.0.0",
"postcss-short-size": "4.0.0",
Expand Down
9 changes: 7 additions & 2 deletions packages/next/build/webpack-config.ts
@@ -1,7 +1,7 @@
import { codeFrameColumns } from '@babel/code-frame'
import ReactRefreshWebpackPlugin from '@next/react-refresh-utils/ReactRefreshWebpackPlugin'
import crypto from 'crypto'
import { readFileSync } from 'fs'
import { readFileSync, realpathSync } from 'fs'
import chalk from 'next/dist/compiled/chalk'
import semver from 'next/dist/compiled/semver'
import TerserPlugin from 'next/dist/compiled/terser-webpack-plugin'
Expand Down Expand Up @@ -665,7 +665,12 @@ export default async function getBaseWebpackConfig(
// Same as above: if the package, when required from the root,
// would be different from what the real resolution would use, we
// cannot externalize it.
if (baseRes !== res) {
if (
!baseRes ||
(baseRes !== res &&
// if res and baseRes are symlinks they could point to the the same file
realpathSync(baseRes) !== realpathSync(res))
) {
return callback()
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/package-managers-tests/basic-pnpm/pages/about.js
@@ -0,0 +1,3 @@
export default function About() {
return <div>About us</div>
}
3 changes: 3 additions & 0 deletions test/package-managers-tests/basic-pnpm/pages/about2.js
@@ -0,0 +1,3 @@
export default function About2() {
return <div>About 2</div>
}
3 changes: 3 additions & 0 deletions test/package-managers-tests/basic-pnpm/pages/day/index.js
@@ -0,0 +1,3 @@
export default function Day() {
return <div>Hello Day</div>
}
11 changes: 11 additions & 0 deletions test/package-managers-tests/basic-pnpm/pages/index.js
@@ -0,0 +1,11 @@
import Link from 'next/link'
export default function Home() {
return (
<div>
Hello World.{' '}
<Link href="/about">
<a>About</a>
</Link>
</div>
)
}
74 changes: 74 additions & 0 deletions test/package-managers-tests/basic-pnpm/test/index.test.js
@@ -0,0 +1,74 @@
/* eslint-env jest */
import { execSync } from 'child_process'
import fs from 'fs-extra'
import path from 'path'

jest.setTimeout(100 * 1000)

beforeAll(async () => {
await clean()
})

afterAll(async () => {
await clean()
})

test('pnpm installs', async () => {
const pnpm = getStdout('yarn bin pnpm')
exec(pnpm + ' init -y')
exec(pnpm + ' add next react react-dom')
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, these pnpm tests should execute somewhere isolated from the monorepo's own node_modules. This helps prevent issues where a module is mistakenly imported from the monorepo node_modules instead of pnpm's node_modules, leading to false test results.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no way to do that other than modifying the CI script, making it even more brittle

Furthermore, the test installs the dependencies before starting, the installed node_modules will always have precedence over the root node_modules

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair.

I'm more worried about cases where a dependency is used by the Next.js app under test, but is missing from the pnpm node_modules tree.

For example, if we forget to install react or another peer dependency, then it will instead be imported from the monorepo node_modules.

A more likely scenario is that a dependency is mistakenly omitted from the next package's dependencies list, e.g. #8344 (this example is for "next-server", but the same could apply to "next"). We want this to cause the pnpm integration test to fail, but if it's not isolated from the monorepo node_modules, then the test will pass.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is no way to do that other than modifying the CI script

Is it possible to have this test create a temp folder outside of the next.js project tree, copy over package.json and pages and run the pnpm commands on that folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes i can do that

await useLocalNextjs()
// exec('pnpm install')
})

test('nextjs builds with pnpm', () => {
const pnpx = getStdout(`yarn bin pnpx`)
exec(pnpx + ' next build')
})

const exec = (cmd) => {
return execSync(cmd, {
env: process.env,
shell: true,
stdio: 'inherit',
cwd: path.resolve(__dirname, '..'),
})
}

const getStdout = (cmd) => {
return execSync(cmd, {
env: process.env,
shell: true,
stdio: 'pipe',
})
.toString()
.trim()
}

async function useLocalNextjs() {
Copy link
Member

Choose a reason for hiding this comment

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

This function looks incorrect as it only considers packages/next but Next.js has a dependency of other packages in the monorepo as well which would not be copied, so this would break eventually.

Copy link
Member

Choose a reason for hiding this comment

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

Can you fix that in a follow-up PR as I'll merge this one.

// installed nextjs
const nextPath = path.dirname(require.resolve('next/package.json'))

// repository root
const root = path.dirname(
require.resolve(path.resolve(process.cwd(), 'package.json'))
)

// local nextjs
const currentNextPath = path.resolve(root, 'packages/next')

// copy local nextjs to node_modules
await fs.copy(
path.resolve(currentNextPath, 'dist'),
path.resolve(nextPath, 'dist')
)

console.log('copied local nextjs to node_modules')
}

async function clean() {
// jest test cannot be found if a package.json exists in test directory
await fs.remove(path.resolve(__dirname, '../package.json'))
await fs.remove(path.resolve(__dirname, '../node_modules'))
await fs.remove(path.resolve(__dirname, '../pnpm-lock.yaml'))
}
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -12567,6 +12567,11 @@ pnp-webpack-plugin@1.6.4:
dependencies:
ts-pnp "^1.1.6"

pnpm@5.8.0:
version "5.8.0"
resolved "https://registry.yarnpkg.com/pnpm/-/pnpm-5.8.0.tgz#a933d6c756efe8795b12004bbff1b82c622e771b"
integrity sha512-J2rAxEXnohwcDkR4KNI6UsYhDs9hJ/tje/BahHpXawi406pmxd6caJUXfRxZPbKvKdyVqfBRKhlX1vyhYbM8lQ==

posix-character-classes@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
Expand Down