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

fix: ACNA-3239 - hash dependencies not in node_modules #196

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@adobe/aio-lib-core-networking": "^5",
"@adobe/aio-lib-env": "^3.0.0",
"archiver": "^6.0.1",
"dependency-tree": "^11.0.1",
"execa": "^4.0.3",
"folder-hash": "^4.0.4",
"fs-extra": "^11.1.1",
Expand Down
39 changes: 31 additions & 8 deletions src/build-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-runtime
const cloneDeep = require('lodash.clonedeep')
const { getCliEnv } = require('@adobe/aio-lib-env')
const { hashElement } = require('folder-hash')
const crypto = require('node:crypto')
const dependencyTree = require('dependency-tree')

const uniqueArr = (items) => {
return [...new Set(items)]
Expand Down Expand Up @@ -193,8 +195,6 @@ const prepareToBuildAction = async (action, root, dist) => {
return fs.existsSync(path.join(dir, file))
}

const actionDir = path.dirname(actionPath)
const srcHash = await hashElement(actionDir, { folders: { exclude: ['node_modules'] } })
if (isDirectory) {
// make sure package.json exists OR index.js exists
if (!filePathExists(actionPath, 'package.json')) {
Expand Down Expand Up @@ -242,7 +242,6 @@ const prepareToBuildAction = async (action, root, dist) => {
actionName,
legacy: defaultPackage,
outPath,
srcHash,
tempBuildDir,
tempActionName: 'index.js'
}
Expand Down Expand Up @@ -317,20 +316,44 @@ const buildActions = async (config, filterActions, skipCheck = false, emptyDist

// here we should check if there are changes since the last build
const actionPath = path.resolve(config.root, action.function)
// actionDir is the directory of this paticular action
const actionDir = path.dirname(actionPath)
// use dependency-tree to get a list of all dependencies excluding node_modules
const actionDepList = dependencyTree.toList({
filename: actionPath,
directory: actionDir,
filter: path => path.indexOf('node_modules') === -1 // exclude node_modules
})

// generate a hash for each dependency
const depHashes = []
for await (const dep of actionDepList) {
const elem = await hashElement(dep)
// console.log('elem hash = ', elem)
depHashes.push(elem.hash)
}

// get a hash of the current action folder
const srcHash = await hashElement(actionDir, { folders: { exclude: ['node_modules'] } })
// lastBuiltData[actionName] === contentHash
// if the flag to skip is set, then we ALWAYS build

// fullHash is what we will use to compare if the action has changed
// this is a hash of the action folder, and all its dependencies
const fullHash = crypto.createHash('sha256')
fullHash.update(srcHash.hash)
depHashes.forEach(hash => {
fullHash.update(hash)
})
const fullHashDigest = fullHash.digest('hex')

// if the flag to skip is set, then we ALWAYS build (TODO: check for skip before calculating hash)
// if the hash is different, we build
// if the user has specified a filter, we build even if hash is the same, they are explicitly asking for it
// but we don't need to add a case, before we are called, skipCheck is set to true if there is a filter
if (skipCheck || lastBuiltData[actionFullName] !== srcHash.hash) {
if (skipCheck || lastBuiltData[actionFullName] !== fullHashDigest) {
// todo: inform the user that the action has changed and we are rebuilding
// console.log('action has changed since last build, zipping', actionFullName)
aioLogger.debug('action has changed since last build, building ..', actionFullName)
const buildResult = await prepareToBuildAction(action, config.root, distFolder)
buildResult.buildHash = { [actionFullName]: srcHash.hash }
buildResult.buildHash = { [actionFullName]: fullHashDigest }
toBuildList.push(buildResult)
} else {
// inform the user that the action has not changed ???
Expand Down
19 changes: 19 additions & 0 deletions test/__mocks__/dependency-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

module.exports = {
toList ({ filter }) {
return [
'actions/action2/index.js',
'node_modules/dependency-tree/index.js'
].filter(filter)
}
}
16 changes: 16 additions & 0 deletions test/__mocks__/folder-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

module.exports = {
hashElement () {
return { hash: 'asdersd' }
}
}
18 changes: 17 additions & 1 deletion test/build.actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const utils = require('../src/utils')
const buildActions = require('../src/build-actions')
const path = require('path')
const fs = require('fs-extra')

jest.mock('dependency-tree')
const execa = require('execa')
jest.mock('execa')
const deepClone = require('lodash.clonedeep')
Expand Down Expand Up @@ -1163,6 +1163,22 @@ test('should not zip files if the action was built before (skipCheck=false)', as
expect(utils.zip).not.toHaveBeenCalled()
})

test('should not build if required file is not changed', async () => {
addSampleAppFiles()
const config = deepClone(global.sampleAppConfig)
await buildActions(config, ['action'], true)
expect(mockLogger.debug).toHaveBeenCalledWith(
expect.stringContaining('action has changed since last build, building ..'),
expect.any(String)
)
mockLogger.debug.mockClear()
await buildActions(config, ['action'], false)
expect(mockLogger.debug).toHaveBeenCalledWith(
expect.stringContaining('action has not changed')
)
expect(utils.zip).toHaveBeenCalledTimes(1)
})

test('should not delete dist folder when emptyDist=false', async () => {
addSampleAppFiles()
const config = deepClone(global.sampleAppConfig)
Expand Down
Loading