Skip to content

Commit 437fcbc

Browse files
AndrewFarleydschep
authored andcommitted
Bugfix for requirements.txt options ordering (#237)
Fixes #236 @vickeryj Can you please try this? ``` rm -Rf node_modules/serverless-python-requirements npm i github:andrewfarley/serverless-python-requirements#bug-fix-requirements-ordering ```
1 parent 7235b59 commit 437fcbc

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/pip.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ function dockerPathForWin(options, path) {
267267

268268
/** create a filtered requirements.txt without anything from noDeploy
269269
* then remove all comments and empty lines, and sort the list which
270-
* assist with matching the static cache
270+
* assist with matching the static cache. The sorting will skip any
271+
* lines starting with -- as those are typically ordered at the
272+
* start of a file ( eg: --index-url / --extra-index-url )
271273
* @param {string} source requirements
272274
* @param {string} target requirements where results are written
273275
* @param {Object} options
@@ -277,14 +279,24 @@ function generateRequirementsFile(source, target, options) {
277279
const requirements = fse
278280
.readFileSync(source, { encoding: 'utf-8' })
279281
.split(/\r?\n/);
282+
var prepend = [];
280283
const filteredRequirements = requirements.filter(req => {
281284
req = req.trim();
282-
if (req.length == 0 || req[0] == '#') {
285+
if (req.startsWith('#')) {
286+
// Skip comments
287+
return false;
288+
} else if (req.startsWith('--')) {
289+
// If we have options (prefixed with --) keep them for later
290+
prepend.push(req);
283291
return false;
284292
}
285293
return !noDeploy.has(req.split(/[=<> \t]/)[0].trim());
286294
});
287-
filteredRequirements.sort(); // Sort them alphabetically
295+
filteredRequirements.sort(); // Sort remaining alphabetically
296+
// Then prepend any options from above in the same order
297+
for (let item of prepend.reverse()) {
298+
filteredRequirements.unshift(item);
299+
}
288300
fse.writeFileSync(target, filteredRequirements.join('\n'));
289301
}
290302

0 commit comments

Comments
 (0)