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

Add JSONDecodeError handling for get_lockfile_hash #2629

Merged
merged 4 commits into from
Jul 30, 2018

Conversation

mlouielu
Copy link
Contributor

Now will return None when facing JSONDecodeError when loading the lockfile.

do_init will remove the old lockfile and replace it.

Fix #2607

lockfile = self.load_lockfile(expand_env_vars=False)
except json.decoder.JSONDecodeError:
# Lockfile corrupted
return
Copy link
Member

Choose a reason for hiding this comment

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

Better to return an empty string here. A lockfile without hash entry can be treated as corrupt as well.

Also JSONDecodeError was added in 3.5, and not available on 2.7. Catch ValueError instead.

pipenv/core.py Outdated
verbose=verbose,
write=True,
pypi_mirror=pypi_mirror,
)
Copy link
Member

Choose a reason for hiding this comment

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

This do_lock call can be merged with the other branch below.

@uranusjr
Copy link
Member

The logic looks good to me in general, thanks for the work. I made some comments on how the implementation can be improved.

Copy link
Member

@uranusjr uranusjr left a comment

Choose a reason for hiding this comment

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

I think this is good to go.

@uranusjr uranusjr added the PR: awaiting-merge The PR related to this issue has been reviewed and is awaiting merge. label Jul 24, 2018
pipenv/core.py Outdated
)
err=True
)
os.remove(project.lockfile_location)
Copy link
Member

Choose a reason for hiding this comment

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

Wait why are we deleting this?

Copy link
Member

Choose a reason for hiding this comment

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

We intentionally don't explicitly delete lockfiles unless we are finished re-locking and have a new one ready to replace them with. There is never a scenario where it is OK to just delete a lockfile -- it should all be handled in the project internals. Other than that one line I'm good with the changes, but @uranusjr wouldn't this have been addressed by introducing the lockfile decoder?

Copy link
Member

Choose a reason for hiding this comment

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

In this particular case I feel it’s fine to delete the lock file because it is corrupted, but either way would do.

Copy link
Member

Choose a reason for hiding this comment

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

'Corrupted' as in doesn't have a hash in it? Doesn't have content? How does this state occur? Either way I really can't see any reason why we would ever forcibly delete the only file in pipenv we made special functionality to ensure never gets removed without being replaced.

This is super inconsistent. If there is a weird problem in a lockfile users should have a chance to modify or mess with or backup their lockfile unless they successfully ran a new lock, which should be the only thing in all of pipenv that removes lockfiles

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or we can move the original lock file to Pipfile.lock.bak or with timestamps to prevent duplicated, then recreate a lock file and notice the user this situation?

Copy link
Member

Choose a reason for hiding this comment

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

No. Do not manipulate the lockfile. We already have tools that do this correctly when you lock. I see no reason to complicate the process with extra manipulation when we already manage all of this. This is a landmine that secretly deletes something that would already be deleted during locking if it succeeded. If it didn’t succeed it would be a lot more helpful if we don’t randomly manipulate it in just this one spot.

Copy link
Member

Choose a reason for hiding this comment

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

For my particular context “corrupted” means the file cannot be JSON decoded. But the os.remove line is not strictly necessary either way. I’m +0 on removing it.

@techalchemy techalchemy removed the PR: awaiting-merge The PR related to this issue has been reviewed and is awaiting merge. label Jul 25, 2018
@techalchemy
Copy link
Member

I’m considering the example where a user makes a manual change and leaves a hanging comma or some other formatting issue. The whole reason we took the approach we did was to avoid deleting this file. I won’t approve any change that deletes it. If I were a user and you deleted my lockfile when instead I could have just fixed the thing I broke, I would be really annoyed

mlouielu and others added 3 commits July 30, 2018 12:22
Now will return None when facing JSONDecodeError when loading the lockfile.

do_init will remove the old lockfile and replace it.
@uranusjr uranusjr force-pushed the get_lockfile_hash_with_bad_lockfile branch from fa9bff6 to bd13183 Compare July 30, 2018 04:26
@uranusjr uranusjr added the PR: awaiting-merge The PR related to this issue has been reviewed and is awaiting merge. label Jul 30, 2018
@uranusjr uranusjr merged commit 08477e8 into pypa:master Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: awaiting-merge The PR related to this issue has been reviewed and is awaiting merge.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants