Skip to content

Commit e81d9e1

Browse files
authored
feat: Introduce requirePoetryLockFile flag
1 parent 6fbdde1 commit e81d9e1

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ custom:
131131
usePoetry: false
132132
```
133133

134+
Be aware that if no `poetry.lock` file is present, a new one will be generated on the fly. To help having predictable builds,
135+
you can set the `requirePoetryLockFile` flag to true to throw an error when `poetry.lock` is missing.
136+
137+
```yaml
138+
custom:
139+
pythonRequirements:
140+
requirePoetryLockFile: false
141+
```
142+
134143
### Poetry with git dependencies
135144

136145
Poetry by default generates the exported requirements.txt file with `-e` and that breaks pip with `-t` parameter

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ServerlessPythonRequirements {
5757
pipCmdExtraArgs: [],
5858
noDeploy: [],
5959
vendor: '',
60+
requirePoetryLockFile: false,
6061
},
6162
(this.serverless.service.custom &&
6263
this.serverless.service.custom.pythonRequirements) ||

lib/poetry.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,28 @@ async function pyprojectTomlToRequirements(modulePath, pluginInstance) {
2121
generateRequirementsProgress = progress.get(
2222
'python-generate-requirements-toml'
2323
);
24-
generateRequirementsProgress.update(
25-
'Generating requirements.txt from "pyproject.toml"'
26-
);
27-
log.info('Generating requirements.txt from "pyproject.toml"');
24+
}
25+
26+
const emitMsg = (msg) => {
27+
if (generateRequirementsProgress) {
28+
generateRequirementsProgress.update(msg);
29+
log.info(msg);
30+
} else {
31+
serverless.cli.log(msg);
32+
}
33+
};
34+
35+
if (fs.existsSync('poetry.lock')) {
36+
emitMsg('Generating requirements.txt from poetry.lock');
2837
} else {
29-
serverless.cli.log('Generating requirements.txt from pyproject.toml...');
38+
if (options.requirePoetryLockFile) {
39+
throw new serverless.classes.Error(
40+
'poetry.lock file not found - set requirePoetryLockFile to false to ' +
41+
'disable this error',
42+
'MISSING_REQUIRED_POETRY_LOCK'
43+
);
44+
}
45+
emitMsg('Generating poetry.lock and requirements.txt from pyproject.toml');
3046
}
3147

3248
try {

test.js

+20
Original file line numberDiff line numberDiff line change
@@ -1634,3 +1634,23 @@ test('py3.7 can ignore functions defined with `image`', async (t) => {
16341634

16351635
t.end();
16361636
});
1637+
1638+
test('poetry py3.7 fails packaging if poetry.lock is missing and flag requirePoetryLockFile is set to true', async (t) => {
1639+
copySync('tests/poetry', 'tests/base with a space');
1640+
process.chdir('tests/base with a space');
1641+
removeSync('poetry.lock');
1642+
1643+
const path = npm(['pack', '../..']);
1644+
npm(['i', path]);
1645+
const stdout = sls(['package'], {
1646+
env: { requirePoetryLockFile: 'true', slim: 'true' },
1647+
noThrow: true,
1648+
});
1649+
t.true(
1650+
stdout.includes(
1651+
'poetry.lock file not found - set requirePoetryLockFile to false to disable this error'
1652+
),
1653+
'flag works and error is properly reported'
1654+
);
1655+
t.end();
1656+
});

tests/poetry/serverless.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ custom:
1313
slimPatterns: ${file(./slimPatterns.yml):slimPatterns, self:custom.defaults.slimPatterns}
1414
slimPatternsAppendDefaults: ${env:slimPatternsAppendDefaults, self:custom.defaults.slimPatternsAppendDefaults}
1515
dockerizePip: ${env:dockerizePip, self:custom.defaults.dockerizePip}
16+
requirePoetryLockFile: ${env:requirePoetryLockFile, false}
1617
defaults:
1718
zip: false
1819
slimPatterns: false

0 commit comments

Comments
 (0)