cli icon indicating copy to clipboard operation
cli copied to clipboard

Commands run with netlify dev's `--command` flag always exit with code 1. Successful commands should exit with code 0.

Open matobeno1 opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe.

I'm using the --command flag to run integration tests together with Netlify dev server. This way the Netlify dev server stops automatically when tests are done. The exit code is always 1 (unfortunately), even if all tests succeed. Here is an example of the command I'm running: NODE_ENV=test netlify dev --command=ava.

Describe the solution you'd like

I'd like the netlify dev to return exit code 0 if the command that was executed by --command flag returns 0.

Describe alternatives you've considered

I've made slight modification using the patch package in my node_modules, inside of netlify-cli folder.

Additional context

This seems to be caused by this line of code.

Can you submit a pull request?

Yes, I have a change prepared in a forked repo. Some tests however keep failing:

➜  cli git:(feature/exit-code-zero-on-successful-command) ✗ ava tests/framework-detection.test.js --match "should pass framework-info env to framework sub process"

Starting dev server on port: 17478 in directory site-with-gatsby
  should pass framework-info env to framework sub process

  tests/framework-detection.test.js:301

   300:     const error = await t.throwsAsync(() => withDevServer({ cwd: builder.directory }, () => {}, true))
   301:     t.snapshot(normalize(error.stdout))                                                               
   302:   })                                                                                                  

  Rejected promise returned by test. Reason:

  TypeError {
    message: 'Cannot read properties of undefined (reading \'replace\')',
  }

  › tests/utils/snapshots.js:17:55
  › Array.reduce (<anonymous>)
  › normalize (tests/utils/snapshots.js:17:15)
  › tests/framework-detection.test.js:301:16
  › withSiteBuilder (tests/utils/site-builder.js:188:12)
  › tests/framework-detection.test.js:288:3

  ─

  1 test failed

Pull requests are welcome! If you would like to help us add this feature, please check our contributions guidelines.

matobeno1 avatar Jan 20 '22 14:01 matobeno1

@matobeno1 do you suggest reusing the status code of the command?

CleanShot 2022-01-24 at 12 51 20@2x

So I guess you mean updating: https://github.com/netlify/cli/blob/main/src/commands/dev/dev.js#L110


     )
    } else {
      const errorMessage = result.failed
        ? `${NETLIFYDEVERR} ${result.shortMessage}`
        : `${NETLIFYDEVWARN} "${command}" exited with code ${result.exitCode}`

      log(`${errorMessage}. Shutting down Netlify Dev server`)
    }
+    process.exit(result.exitCode)
-    process.exit(1)
  })

would this be enough for you?

lukasholzer avatar Jan 24 '22 11:01 lukasholzer

Yes it would be great, thank you!

matobeno1 avatar Jan 24 '22 15:01 matobeno1

To add more context, the command netlify dev runs is expected to be a long running command that doesn't exit at all (e.g. a server), hence if it exists it consists as a failure.

This is useful if a user configures the wrong command to signal an error.

I wonder if using something like https://github.com/bahmutov/start-server-and-test will be more straightforward for this specific use case.

erezrokah avatar Jan 24 '22 16:01 erezrokah