Skip to content

[Bug]: publishDir incorrect when deploying using Netlify CLI with cwd option #1258

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

Closed
2 tasks done
lourd opened this issue Mar 10, 2022 · 1 comment · Fixed by #1264
Closed
2 tasks done

[Bug]: publishDir incorrect when deploying using Netlify CLI with cwd option #1258

lourd opened this issue Mar 10, 2022 · 1 comment · Fixed by #1264
Labels
type: bug code to address defects in shipped code

Comments

@lourd
Copy link

lourd commented Mar 10, 2022

Summary

My team has an Nx-managed monorepo. We recently started deploying our web app to Netlify using the CLI. Up till now we've gotten by with having our netlify.toml in the root of our repo, but we're trying to colocate that config with the app in order to allow for multiple web apps to be deployed from the monorepo. In pursuit of that, I've been trying to use the lesser-known cwd option that the CLI provides.

Long-story short, using the cwd option does not work with the Next.js build plugin.

There's two problems. The first appears to be with the Netlify CLI not passing the cwd option along, which affected how the publish directory was set. I debugged that and made a patch, see this commit.

Fixing that allowed the build and deploy to finish. But then the deploy preview has an error on any page that hits one of the back-end functions about not being able to find required modules. For example, see this link https://622969f742f98859a319f7ab--suspicious-shannon-5bd1ac.netlify.app/fooooooo.

Steps to reproduce

  1. Generate an Nx workspace
  2. Generate a Next.js app in the workspace
  3. Add a netlify.toml file to the app's directory
  4. Try to build & deploy using the netlify CLI and the cwd flag. See here in the reproduction repo for example.

This fails:

Error: The directory "../.." does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
  1. After applying this patch, try building and deploying again. This should succeed.
  2. On the deployed app, do something that will require one of the back-end functions, like going to a route that doesn't exist. Netlify returns a 502 error with this content:
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module '../../../Users/louis/code/netlify-cli-repro/dist/apps/toy-app/.next/required-server-files.json'\nRequire stack:\n- /var/task/apps/toy-app/.netlify/functions-internal/___netlify-handler/___netlify-handler.js\n- /var/task/___netlify-handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module '../../../Users/louis/code/netlify-cli-repro/dist/apps/toy-app/.next/required-server-files.json'",
    "Require stack:",
    "- /var/task/apps/toy-app/.netlify/functions-internal/___netlify-handler/___netlify-handler.js",
    "- /var/task/___netlify-handler.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:202:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
    "    at Module.load (internal/modules/cjs/loader.js:950:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

A link to a reproduction repository

https://github.com/lourd/netlify-cli-repro

Plugin version

4.2.7

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
[build]
  command = "yarn build toy-app"
  publish = "../../dist/apps/toy-app/.next"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
// eslint-disable-next-line @typescript-eslint/no-var-requires
const withNx = require('@nrwl/next/plugins/with-nx');

/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/
const nextConfig = {
  nx: {
    // Set this to true if you would like to to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: false,
  },
};

module.exports = withNx(nextConfig);

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.
@lourd lourd added the type: bug code to address defects in shipped code label Mar 10, 2022
@lourd lourd changed the title [Bug]: Cannot deploy multiple sites from Nx monorepo using CLI [Bug]: publishDir incorrect when deploying using Netlify CLI with cwd option Mar 15, 2022
@lourd
Copy link
Author

lourd commented Mar 15, 2022

Looking at things a little more closely, the problematic logic seems to be these two lines

https://github.com/netlify/netlify-plugin-nextjs/blob/02053fdce786e26a5a6c60a9e38b9e05fd2ac0d3/src/helpers/functions.ts#L16-L17

Assuming that the cwd is process.cwd(). When building without the cwd option, the publishDir value is ../../../dist/apps/toy-app/.next, but with the cwd option, it's ../../../Users/louis/code/netlify-cli-repro/dist/apps/toy-app/.next. The value should be ../../../../../dist/apps/toy-app/.next, the same value as before with two additional upwards path traversals, since the .netlify directory is being generated in apps/toy-app instead of .netlify at the root of the repo.

@ascorbic, sorry to ping you directly, but this is a kind of a significant blocker for my team. Please let me know if there's anything more I can do to help with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant