Skip to content

Commit c723f7c

Browse files
authored
Merge pull request #175 from gadget-inc/fix-workspace-watches
Add tests to ensure cross-workspace reloading works ok
2 parents 1452b72 + 373ec4c commit c723f7c

27 files changed

+361
-96
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_modules
22
pkg
33
.envrc.local
4-
test/supervise/run-scratch.ts
5-
test/reload/run-scratch.ts
4+
**/run-scratch.ts
5+
**/run-scratch.ts
66
.direnv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"module": {
3+
"type": "commonjs",
4+
"lazy": true
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "main",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "commonjs",
6+
"dependencies": {
7+
"side": "workspace:*"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import http from "http";
2+
import { message } from "side/run-scratch";
3+
4+
const requestListener = function (req, res) {
5+
res.writeHead(200);
6+
res.end(message);
7+
};
8+
9+
const server = http.createServer(requestListener);
10+
server.listen(8080);
11+
console.warn("Listening on 8080");

integration-test/reload-cross-workspace-lazy/pnpm-lock.yaml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages:
2+
- "main"
3+
- "side"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"module": {
3+
"type": "commonjs",
4+
"lazy": true
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "side",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "commonjs"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const message = "Hello, World!";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
3+
set -e
4+
5+
6+
# kill the server when this script exits
7+
trap "kill -9 0" INT TERM
8+
trap 'kill $(jobs -p)' EXIT
9+
10+
# setup the pnpm workspace with multiple packages
11+
cd $DIR
12+
pnpm install
13+
14+
# make a copy of the run.ts file in the side package for us to modify
15+
cp $DIR/side/run.ts $DIR/side/run-scratch.ts
16+
17+
# run a server in the main package in the background
18+
$DIR/../../pkg/wds.bin.js $@ --watch --commands $DIR/main/run.ts &
19+
20+
max_retry=5
21+
counter=0
22+
23+
set +e
24+
until curl -s localhost:8080 | grep "World"
25+
do
26+
sleep 1
27+
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
28+
echo "Trying again. Try #$counter"
29+
((counter++))
30+
done
31+
32+
echo "Made initial request to server"
33+
34+
# modify the file in the side package and expect the main script to reload
35+
sed -i 's/Hello, World/Hey, Pluto/g' $DIR/side/run-scratch.ts
36+
37+
echo "Made change to side package"
38+
39+
counter=0
40+
until curl -s localhost:8080 | grep "Pluto"
41+
do
42+
sleep 1
43+
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
44+
echo "Trying again. Try #$counter"
45+
((counter++))
46+
done
47+
48+
echo "Made new request to reloaded server"
49+
50+
exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "main",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "module",
6+
"dependencies": {
7+
"side": "workspace:*"
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import http from "http";
2+
import { message } from "side/run-scratch";
3+
4+
const requestListener = function (req, res) {
5+
res.writeHead(200);
6+
res.end(message);
7+
};
8+
9+
const server = http.createServer(requestListener);
10+
server.listen(8080);
11+
console.warn("Listening on 8080");

integration-test/reload-cross-workspace/pnpm-lock.yaml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages:
2+
- "main"
3+
- "side"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "side",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "module"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const message = "Hello, World!";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bash
2+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
3+
set -e
4+
5+
6+
# kill the server when this script exits
7+
trap "kill -9 0" INT TERM
8+
trap 'kill $(jobs -p)' EXIT
9+
10+
# setup the pnpm workspace with multiple packages
11+
cd $DIR
12+
pnpm install
13+
14+
# make a copy of the run.ts file in the side package for us to modify
15+
cp $DIR/side/run.ts $DIR/side/run-scratch.ts
16+
17+
# run a server in the main package in the background
18+
$DIR/../../pkg/wds.bin.js $@ --watch --commands $DIR/main/run.ts &
19+
20+
max_retry=5
21+
counter=0
22+
23+
set +e
24+
until curl -s localhost:8080 | grep "World"
25+
do
26+
sleep 1
27+
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
28+
echo "Trying again. Try #$counter"
29+
((counter++))
30+
done
31+
32+
echo "Made initial request to server"
33+
34+
# modify the file in the side package and expect the main script to reload
35+
sed -i 's/Hello, World/Hey, Pluto/g' $DIR/side/run-scratch.ts
36+
37+
echo "Made change to side package"
38+
39+
counter=0
40+
until curl -s localhost:8080 | grep "Pluto"
41+
do
42+
sleep 1
43+
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
44+
echo "Trying again. Try #$counter"
45+
((counter++))
46+
done
47+
48+
echo "Made new request to reloaded server"
49+
50+
exit 0

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@
4040
"node": ">=16.0.0"
4141
},
4242
"dependencies": {
43+
"@pnpm/find-workspace-dir": "^1000.0.0",
4344
"@swc/core": "^1.9.3",
4445
"@swc/helpers": "^0.5.13",
4546
"find-root": "^1.1.0",
4647
"find-yarn-workspace-root": "^2.0.0",
4748
"fs-extra": "^11.2.0",
4849
"globby": "^11.1.0",
4950
"lodash": "^4.17.20",
50-
"oxc-resolver": "^1.12.0",
51+
"micromatch": "^4.0.8",
5152
"node-object-hash": "^3.0.0",
53+
"oxc-resolver": "^1.12.0",
5254
"pkg-dir": "^5.0.0",
5355
"watcher": "^2.3.1",
5456
"write-file-atomic": "^6.0.0",
@@ -61,6 +63,7 @@
6163
"@types/find-root": "^1.1.4",
6264
"@types/fs-extra": "^11.0.4",
6365
"@types/lodash": "^4.17.13",
66+
"@types/micromatch": "^4.0.9",
6467
"@types/node": "^22.9.3",
6568
"@types/write-file-atomic": "^4.0.3",
6669
"@types/yargs": "^15.0.19",

pnpm-lock.yaml

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/SwcCompiler.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ test("throws if the file is ignored", async () => {
4141
}
4242
}
4343

44-
expect(error).toBeDefined();
44+
expect(error).toBeTruthy();
4545
expect(error?.ignoredFile).toBeTruthy();
4646
expect(error?.message).toMatch(
47-
/File .+ignored\.ts is imported but not being built because it is explicitly ignored in the wds project config\. It is being ignored by the provided glob pattern '!ignored\.ts', remove this pattern from the project config or don't import this file to fix./
47+
/File .+ignored\.ts is imported but not being built because it is explicitly ignored in the wds project config\. It is being ignored by the provided glob pattern 'ignored\.ts', remove this pattern from the project config or don't import this file to fix./
4848
);
4949
});
5050

src/Options.ts

-17
This file was deleted.

src/Project.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from "lodash";
22
import type { Compiler } from "./Compiler.js";
3-
import type { ProjectConfig } from "./Options.js";
43
import { PathTrie } from "./PathTrie.js";
4+
import type { ProjectConfig } from "./ProjectConfig.js";
55
import type { Supervisor } from "./Supervisor.js";
66
import { log } from "./utils.js";
77

0 commit comments

Comments
 (0)