Skip to content

Commit 0030936

Browse files
author
bweigel
committed
fix weird caching bug
1 parent 6255b16 commit 0030936

File tree

8 files changed

+202
-32
lines changed

8 files changed

+202
-32
lines changed

lib/pip.js

+23-25
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,19 @@ function mergeCommands(commands) {
5353
* @param {Object} options
5454
* @return {undefined}
5555
*/
56-
function installRequirementsFile(
56+
function generateRequirementsFile(
5757
requirementsPath,
5858
targetFile,
5959
serverless,
6060
servicePath,
6161
options
6262
) {
6363
if (options.usePipenv && fse.existsSync(path.join(servicePath, 'Pipfile'))) {
64-
generateRequirementsFile(
64+
filterRequirementsFile(
65+
/*
66+
TODO: this is supposed to generate a requirements.txt from Pipfile,
67+
but source and target are basicalle the same
68+
*/
6569
path.join(servicePath, '.serverless/requirements.txt'),
6670
targetFile,
6771
options
@@ -70,7 +74,7 @@ function installRequirementsFile(
7074
`Parsed requirements.txt from Pipfile in ${targetFile}...`
7175
);
7276
} else {
73-
generateRequirementsFile(requirementsPath, targetFile, options);
77+
filterRequirementsFile(requirementsPath, targetFile, options);
7478
serverless.cli.log(
7579
`Generated requirements from ${requirementsPath} in ${targetFile}...`
7680
);
@@ -306,7 +310,6 @@ function dockerPathForWin(path) {
306310
return path;
307311
}
308312
}
309-
310313
/** create a filtered requirements.txt without anything from noDeploy
311314
* then remove all comments and empty lines, and sort the list which
312315
* assist with matching the static cache. The sorting will skip any
@@ -318,7 +321,7 @@ function dockerPathForWin(path) {
318321
* @param {string} target requirements where results are written
319322
* @param {Object} options
320323
*/
321-
function generateRequirementsFile(source, target, options) {
324+
function filterRequirementsFile(source, target, options) {
322325
const noDeploy = new Set(options.noDeploy || []);
323326
const requirements = fse
324327
.readFileSync(source, { encoding: 'utf-8' })
@@ -413,11 +416,21 @@ function installRequirementsIfNeeded(
413416
}
414417
}
415418

416-
// First, generate the requirements file to our local .serverless folder
417-
fse.ensureDirSync(path.join(servicePath, '.serverless'));
418-
const slsReqsTxt = path.join(servicePath, '.serverless', 'requirements.txt');
419+
let requirementsTxtDirectory;
420+
// Copy our requirements to another path in .serverless (incase of individually packaged)
421+
if (modulePath && modulePath != '.') {
422+
requirementsTxtDirectory = path.join(
423+
servicePath,
424+
'.serverless',
425+
modulePath
426+
);
427+
} else {
428+
requirementsTxtDirectory = path.join(servicePath, '.serverless');
429+
}
430+
fse.ensureDirSync(requirementsTxtDirectory);
431+
const slsReqsTxt = path.join(requirementsTxtDirectory, 'requirements.txt');
419432

420-
installRequirementsFile(
433+
generateRequirementsFile(
421434
fileName,
422435
slsReqsTxt,
423436
serverless,
@@ -433,28 +446,13 @@ function installRequirementsIfNeeded(
433446
return false;
434447
}
435448

436-
// Copy our requirements to another filename in .serverless (incase of individually packaged)
437-
if (modulePath && modulePath != '.') {
438-
fse.existsSync(path.join(servicePath, '.serverless', modulePath));
439-
const destinationFile = path.join(
440-
servicePath,
441-
'.serverless',
442-
modulePath,
443-
'requirements.txt'
444-
);
445-
serverless.cli.log(
446-
`Copying from ${slsReqsTxt} into ${destinationFile} ...`
447-
);
448-
fse.copySync(slsReqsTxt, destinationFile);
449-
}
450-
451449
// Then generate our MD5 Sum of this requirements file to determine where it should "go" to and/or pull cache from
452450
const reqChecksum = md5Path(slsReqsTxt);
453451

454452
// Then figure out where this cache should be, if we're caching, if we're in a module, etc
455453
const workingReqsFolder = getRequirementsWorkingPath(
456454
reqChecksum,
457-
servicePath,
455+
requirementsTxtDirectory,
458456
options
459457
);
460458

lib/shared.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ function checkForAndDeleteMaxCacheVersions(options, serverless) {
5757
* @param {Object} options
5858
* @return {string}
5959
*/
60-
function getRequirementsWorkingPath(subfolder, servicePath, options) {
60+
function getRequirementsWorkingPath(
61+
subfolder,
62+
requirementsTxtDirectory,
63+
options
64+
) {
6165
// If we want to use the static cache
6266
if (options && options.useStaticCache) {
6367
if (subfolder) {
@@ -69,7 +73,7 @@ function getRequirementsWorkingPath(subfolder, servicePath, options) {
6973
}
7074

7175
// If we don't want to use the static cache, then fallback to the way things used to work
72-
return path.join(servicePath, '.serverless', 'requirements');
76+
return path.join(requirementsTxtDirectory, 'requirements');
7377
}
7478

7579
/**

0 commit comments

Comments
 (0)