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

feat: Introduce requirePoetryLockFile flag #728

Merged

Conversation

FinchPowers
Copy link
Contributor

@FinchPowers FinchPowers commented Sep 28, 2022

  • Distinct logging whether the requirements.txt file is generated from poetry.lock vs pyproject.toml.
  • New boolean flag: requirePoetryLockFile - When set to true, fail the build when poetry.lock is missing. This helps with creating reproducible builds where you want to have a fixed poetry.lock file instead of one generated on the fly.

Follow up to #639

lib/poetry.js Outdated
Comment on lines 26 to 33
const emitMsg = (msg) => {
if (generateRequirementsProgress) {
generateRequirementsProgress.update(msg)
log.info(msg);
} else {
serverless.cli.log(msg);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am less certain about this part. I extracted what looked like the logging logic.

Copy link
Collaborator

@pgrzesik pgrzesik left a comment

Choose a reason for hiding this comment

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

Hey @FinchPowers, thanks a lot for proposing this. I think the idea is good, however, I have a few comments and it seems like the CI builds are failing. Could you please look into that? Thanks!

lib/poetry.js Outdated
}

try {
await spawn("test", ["-f", "poetry.lock"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is that command cross-platform?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess not! :) I'll have a look.

Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks 🙇

lib/poetry.js Outdated
emitMsg('Generating requirements.txt from poetry.lock...');
} catch (e) {
if (options.requirePoetryLockFile) {
throw new Error(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not throw such errors, use the Error class passed from the Serverless class (serverless.classes.Error).

lib/poetry.js Outdated
);
}
emitMsg(
'Generating poetry.lock and requirements.txt from pyproject.toml...'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's not put the dots at the end - it can update an interactive spinner which already indicates that something is happening

@pgrzesik
Copy link
Collaborator

And I forgot - it would be great to cover that functionality with a test case 💯

@FinchPowers FinchPowers force-pushed the feature/log-and-require-poetry-lock branch from cf627ff to 166ae65 Compare October 1, 2022 01:12
@FinchPowers
Copy link
Contributor Author

@pgrzesik Thanks for the pointers, I've updated the code.

For testing, I can, but I'll need some pointers. Is it using a testing framework? From a quick glance at the code it seems to be a custom implementation.

How do you suggest I approach this? I see two options:

  1. Use a filesystem mocker to hide tests/poetry/poetry.lock and invoke the package on it with requirePoetryLockFile set to true.

or

  1. At testing time, copy tests/poetry to tests/poetry-no-lock, remove the lock file from it and invoke the package on it with requirePoetryLockFile set to true.

@pgrzesik
Copy link
Collaborator

pgrzesik commented Oct 1, 2022

Hey @FinchPowers, thanks a lot 🙇 As for testing, it's using tape, but it's pretty barebones as you've noticed and kinda looks like a custom implementation. I don't have much experience with it as well, but I think it would be best to avoid mocking, so something similar to your suggestion 2 would be best 👍 I don't think you will have to copy poetry anywhere, but you should be able to just use that "fixture" as each test is run separately and in isolation (see setup and teardown methods). If you have any questions, please let me know 💯

@FinchPowers FinchPowers force-pushed the feature/log-and-require-poetry-lock branch 3 times, most recently from 794a646 to 24e2d2c Compare October 5, 2022 01:38
Copy link
Collaborator

@pgrzesik pgrzesik left a comment

Choose a reason for hiding this comment

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

Hey @FinchPowers, it looks great overall 🙌 I have only two minor suggestions, please let me know what do you think 🙇

}
};

if (fs.existsSync('poetry.lock')) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's hide this check behind the requirePoetryLockFile, there's no need to check if for users that don't even use this flag

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two different accurate logging messages with that check though. This is the thing that brought me to open this PR: When I started working with the serverless-python-requirements plugin and saw the former message, I was unsure what it was actually doing so I had to open the code to verify.

That being said, tell me what you want me to do. :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @FinchPowers, I'm not sure I understand. Why do you think it makes sense to check it for each user, even the ones that don't use the introduced flag? Maybe I'm missing something here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's to provide accurate logging. Let's say the flag does not exist, that would still leave this

if (fs.existsSync('poetry.lock')) {
  emitMsg('Generating requirements.txt from poetry.lock');
} else {
  emitMsg('Generating poetry.lock and requirements.txt from pyproject.toml');
}

The distinction is important. In the former case, you end up with a predictable requirements.txt which means that you have a repeatable build. In the later, you don't, it depends on what time the command was run and what was available of that time. (Unless all dependencies are locked to a specific version in pyproject.toml, which is rarely the case.) So if you rebuild to troubleshoot, you may endup with different dependencies and not be able to reproduce.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pgrzesik Standing by: Tell me what you would like me to do. :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @FinchPowers 👋 Sorry for not responding sooner, I was on a short vacation for pretty much the whole last week.

That makes a lot of sense, thanks for the explanation 👍

lib/poetry.js Show resolved Hide resolved
* Distinct logging whether the requirements.txt file is generated from
  poetry.lock vs pyproject.toml.
* New boolean flag: requirePoetryLockFile - When set to true, fail the build
  when poetry.lock is missing. This helps with creating reproducible
  builds where you want to have a fixed poetry.lock file instead of one
  generated on the fly.
@FinchPowers FinchPowers force-pushed the feature/log-and-require-poetry-lock branch from 24e2d2c to 3ddf189 Compare October 14, 2022 01:16
Copy link
Collaborator

@pgrzesik pgrzesik left a comment

Choose a reason for hiding this comment

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

Looking great @FinchPowers, thanks a lot 🙇 Let's wait for tests to pass and I'll merge the PR

@pgrzesik pgrzesik changed the title Enhance poetry related logging and add new flag feat: Introduce requirePoetryLockFile flag Oct 18, 2022
@pgrzesik pgrzesik merged commit e81d9e1 into serverless:master Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants