netlify / cli

Netlify Command Line Interface
http://cli.netlify.com
MIT License
1.58k stars 358 forks source link

Wrong `state.json` is being used #1160

Open ehmicky opened 4 years ago

ehmicky commented 4 years ago

This was initially reported by @biilmann.

He created a completely fresh project with just a public folder, a functions folder and a netlify.toml file and then did a netlify deploy --prod and instead of creating a new site or prompting for an existing one, it deployed to the last project he worked on and overwrote the product deploy there. It seems the CLI is writing the site state.json to the .netlify folder in my home directory instead of in the site specific directory.

He is not sure how he got into that state because it is so hard to debug. For some reason, setting up a project ended up writing to his home directory instead of the project dir, and then any new project travels up and finds that state file.

Once he deleted that file things go back to working as expected.

ehmicky commented 4 years ago

It seems like the only place retrieving state.json is here:

https://github.com/netlify/cli/blob/f88b460882a43b63d4997f930722519e9fbdd8bb/src/utils/state-config.js#L17-L25

So, it looks like the situation above would happen for any user who has a ~/.netlify/state.json file on their machine, but no ./.netlify/state.json (e.g. when creating a new Site directory). The question would then become: how did @biilmann end up having a ~/.netlify/state.json file? Maybe he ran some netlify CLI commands with the home directory being the current directory: based on the logic above, the current directory is the default value, so maybe that command created it?

@erezrokah What are your thoughts on this issue?

erezrokah commented 4 years ago

Thanks @ehmicky for looking into it. I think it would make sense to ignore the home directory when looking for state.json. I don't think it is a valid use case to have a site in the home directory, WDYT?

ehmicky commented 4 years ago

Yes, I agree.

When a user is running a netlify CLI command and cwd is the home directory, what should happen?

The current code is assuming that there is always a valid .netlify/state.json that can be read/written for any netlify CLI command invocation.

Several possible solutions:

  1. Throwing when the current directory is the home directory (and maybe also the root directory).
  2. Throwing when .netlify/state.json is about to be written to, and the current directory is the home directory. This seems to happen only with the npm init and npm link commands.
  3. Using a temporary (in-memory), empty state.json for the duration of the command

What do you think?

erezrokah commented 4 years ago

We can divide the logic:

  1. What happens when we find a state file in the home directory? Possible solution - warn the user but allow to continue via a prompt. On CI we can throw.
  2. What happens when the user tries to initialise a project in the home directory? Possible solution - warn but allow via prompt. On CI we can throw.
Leksat commented 2 months ago

Once I created a new site via a globally installed netlify-cli. ~/.netlify/state.json was generated silently; I was not aware of that.

Later, I found that one of my tests was failing locally. This time, netlify-cli was installed via the project's package.json.

Comparing --debug output from local vs CI showed me this: Screenshot_2024-09-13_at_12_37_54-3

baseRelDir was different. This led to the edge_functions path being incorrect.

Somehow, I was able to guess that I needed to remove ~/.netlify/state.json. It was very confusing 😅