Skip to content

Commit

Permalink
Make Single Package repo work with globalDependencies config (vercel#…
Browse files Browse the repository at this point in the history
…4240)

Co-authored-by: Stefano Faieta <[email protected]>
  • Loading branch information
mehulkar and stefanofa authored Mar 23, 2023
1 parent ef25cac commit 3bd81e3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 deletions.
11 changes: 6 additions & 5 deletions cli/integration_tests/single_package/dry-run.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ Check
$ ${TURBO} run build --dry --single-package

Global Hash Inputs
Global Files = 2
Global Files = 3
External Dependencies Hash =
Global Cache Key = Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
Root pipeline = {"//#build":{"outputs":["foo"],"cache":true,"dependsOn":[],"inputs":[],"outputMode":"full","env":[],"persistent":false}}

Tasks to Run
build
Task = build
Hash = 7bf32e1dedb04a5d
Hash = dd4a9a7b508b0e38
Cached (Local) = false
Cached (Remote) = false
Command = echo 'building' > foo
Outputs = foo
Log File = .turbo/turbo-build.log
Dependencies =
Dependendents =
Inputs Files Considered = 4
Inputs Files Considered = 5
Configured Environment Variables =
Inferred Environment Variables =
Global Environment Variables = VERCEL_ANALYTICS_ID=
Expand All @@ -34,7 +34,7 @@ Check
"tasks": [
{
"task": "build",
"hash": "7bf32e1dedb04a5d",
"hash": "dd4a9a7b508b0e38",
"cacheState": {
"local": false,
"remote": false
Expand Down Expand Up @@ -63,7 +63,8 @@ Check
".gitignore": "6f23ff6842b5526da43ab38f4a5bf3b0158eeb42",
"package-lock.json": "8db0df575e6509336a6719094b63eb23d2c649c1",
"package.json": "185771929d92c3865ce06c863c07d357500d3364",
"turbo.json": "2b9b71e8eca61cda6f4c14e07067feac9c1f9862"
"somefile.txt": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"turbo.json": "505752e75c10f9e7a0d2538cf8b6f0fcfb8980a0"
},
"expandedOutputs": [],
"framework": "\u003cNO FRAMEWORK DETECTED\u003e",
Expand Down
1 change: 1 addition & 0 deletions cli/integration_tests/single_package/my-pkg/somefile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
1 change: 1 addition & 0 deletions cli/integration_tests/single_package/my-pkg/turbo.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["somefile.txt"],
"pipeline": {
"build": {
"outputs": ["foo"]
Expand Down
35 changes: 35 additions & 0 deletions cli/integration_tests/single_package/run-yarn.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Setup
$ . ${TESTDIR}/../setup.sh
$ . ${TESTDIR}/setup.sh $(pwd) "[email protected]"
$ rm -rf package-lock.json || true # exists because of setup.sh script above
$ yarn install > /dev/null 2>&1
$ git commit --quiet -am "Update lockfile" # clean git state

Check
$ ${TURBO} run build
\xe2\x80\xa2 Running build (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
build: cache miss, executing 13cea0f56e6c743e
build: yarn run v1.22.17
build: warning package.json: No license field
build: $ echo 'building' > foo
build: Done in \s*[\.0-9]+m?s\. (re)

Tasks: 1 successful, 1 total
Cached: 0 cached, 1 total
Time:\s*[\.0-9]+m?s (re)


$ ${TURBO} run build
\xe2\x80\xa2 Running build (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
build: cache hit, replaying output 13cea0f56e6c743e
build: yarn run v1.22.17
build: warning package.json: No license field
build: $ echo 'building' > foo
build: Done in \s*[\.0-9]+m?s\. (re)

Tasks: 1 successful, 1 total
Cached: 1 cached, 1 total
Time:\s*[\.0-9]+m?s >>> FULL TURBO (re)

4 changes: 2 additions & 2 deletions cli/integration_tests/single_package/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Check
$ ${TURBO} run build --single-package
\xe2\x80\xa2 Running build (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
build: cache miss, executing 7bf32e1dedb04a5d
build: cache miss, executing dd4a9a7b508b0e38
build:
build: > build
build: > echo 'building' > foo
Expand All @@ -20,7 +20,7 @@ Run a second time, verify caching works because there is a config
$ ${TURBO} run build --single-package
\xe2\x80\xa2 Running build (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
build: cache hit, replaying output 7bf32e1dedb04a5d
build: cache hit, replaying output dd4a9a7b508b0e38
build:
build: > build
build: > echo 'building' > foo
Expand Down
9 changes: 9 additions & 0 deletions cli/integration_tests/single_package/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
TARGET_DIR=$1
cp -a ${SCRIPT_DIR}/my-pkg/. ${TARGET_DIR}/
${SCRIPT_DIR}/../setup_git.sh ${TARGET_DIR}

if [ "$2" != "" ]; then
# Use jq to write a new file with a .packageManager field set and then
# Overwrite original package.json. For some reason the command above won't send its output
# directly to the original file.
jq --arg pm "$2" '.packageManager = $pm' "$TARGET_DIR/package.json" > "$TARGET_DIR/package.json.new"
mv "$TARGET_DIR/package.json.new" "$TARGET_DIR/package.json"
git commit -am "Update package manager" --quiet
fi
16 changes: 15 additions & 1 deletion cli/internal/packagemanager/yarn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package packagemanager

import (
"errors"
"fmt"
"os/exec"
"path/filepath"
Expand All @@ -12,6 +13,13 @@ import (
"github.com/vercel/turbo/cli/internal/turbopath"
)

// NoWorkspacesFoundError is a custom error used so that upstream implementations can switch on it
type NoWorkspacesFoundError struct{}

func (e *NoWorkspacesFoundError) Error() string {
return "package.json: no workspaces found. Turborepo requires Yarn workspaces to be defined in the root package.json"
}

var nodejsYarn = PackageManager{
Name: "nodejs-yarn",
Slug: "yarn",
Expand All @@ -27,7 +35,7 @@ var nodejsYarn = PackageManager{
return nil, fmt.Errorf("package.json: %w", err)
}
if len(pkg.Workspaces) == 0 {
return nil, fmt.Errorf("package.json: no workspaces found. Turborepo requires Yarn workspaces to be defined in the root package.json")
return nil, &NoWorkspacesFoundError{}
}
return pkg.Workspaces, nil
},
Expand All @@ -42,6 +50,12 @@ var nodejsYarn = PackageManager{

globs, err := pm.getWorkspaceGlobs(rootpath)
if err != nil {
// In case of a non-monorepo, the workspaces field is empty and only node_modules in the root should be ignored
var e *NoWorkspacesFoundError
if errors.As(err, &e) {
return []string{"node_modules/**"}, nil
}

return nil, err
}

Expand Down

0 comments on commit 3bd81e3

Please sign in to comment.