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

Wrong state.json is being used #1160

Open
ehmicky opened this issue Aug 28, 2020 · 5 comments
Open

Wrong state.json is being used #1160

ehmicky opened this issue Aug 28, 2020 · 5 comments

Comments

@ehmicky
Copy link
Contributor

ehmicky commented Aug 28, 2020

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 ehmicky added the type: bug code to address defects in shipped code label Aug 28, 2020
@ehmicky
Copy link
Contributor Author

ehmicky commented Aug 28, 2020

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

findStatePath(cwd) {
const statePath = findUp.sync([STATE_PATH], { cwd })
if (!statePath) {
return path.join(cwd, STATE_PATH)
}
return statePath
}

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
Copy link
Contributor

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
Copy link
Contributor Author

ehmicky commented Aug 31, 2020

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
Copy link
Contributor

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
Copy link

Leksat commented Sep 13, 2024

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 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants