Skip to content

Commit

Permalink
Switch JS package manager to pnpm (#44167)
Browse files Browse the repository at this point in the history
* Adjust `package.json`s to pnpm

* Convert resolutions (pnpm doesn't support `**/` syntax)

It also removes resolutions that are no longer relevant.

* Convert `yarn.lock` to `pnpm-lock.yaml` with `pnpm import`

* Always add `e/web/teleport` workspace to the lockfile

* Adjust `jest` to new node_modules structure

* Adjust `storybook` to new node_modules structure

* Replace `toHaveStyleRule` with `toHaveStyle`

`jest-styled-components` package is no longer global, so `toHaveStyleRule` matchers is not recognized by TS.
We use them only in three places, so it's simpler to convert them to `toHaveStyle`.

* Adjust README files

* Adjust GHA workflows

* Adjust build assets

* Remove remaining `yarn` mentions

I don't think anyone uses the debug configuration for electron (and we don't have `start-electron` script for a long time).

* `corepack enable` -> `corepack enable pnpm`

* Automatically enable pnpm when running `make build/teleport`

* Run `pnpm import` again

* Run `pnpm dedupe`

* Add `yarn.lock` to .gitignore

* Link to pnpm installation docs

* Add a better comment for `transformIgnorePatterns`

* Make pattern for `shared` stories more specific

* Change `corepack enable pnpm` in readmes

* Run `pnpm import && pnpm dedupe` again
  • Loading branch information
gzdunek authored Jul 19, 2024
1 parent f797b86 commit 620c0a0
Show file tree
Hide file tree
Showing 34 changed files with 18,680 additions and 193 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-macos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ jobs:
with:
node-version: ${{ env.NODE_VERSION }}

- name: Setup yarn
- name: Setup pnpm
run: |
corepack enable yarn
corepack enable pnpm
- name: Install Go Toolchain
uses: actions/setup-go@v5
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint-ui-bypass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand All @@ -29,7 +29,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/lint-ui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand All @@ -19,7 +19,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand All @@ -39,17 +39,17 @@ jobs:
run: |
node --version
- name: Install Yarn dependencies
run: bash web/packages/build/scripts/yarn-install-frozen-lockfile.sh
- name: Install JS dependencies
run: pnpm install --frozen-lockfile

- name: Build WASM
run: yarn build-wasm
run: pnpm build-wasm

- name: Run Type Check
run: yarn type-check
run: pnpm type-check

- name: Run lint
run: yarn lint
run: pnpm lint

- name: Run Storybook smoke test
run: yarn storybook-smoke-test
run: pnpm storybook-smoke-test
4 changes: 2 additions & 2 deletions .github/workflows/unit-tests-ui-bypass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand All @@ -32,7 +32,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/unit-tests-ui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand All @@ -22,7 +22,7 @@ on:
- 'gen/proto/js/**'
- 'gen/proto/ts/**'
- 'package.json'
- 'yarn.lock'
- 'pnpm-lock.yaml'
- 'Cargo.toml'
- 'Cargo.lock'
- 'tsconfig.json'
Expand All @@ -46,11 +46,11 @@ jobs:
run: |
node --version
- name: Install Yarn dependencies
run: bash web/packages/build/scripts/yarn-install-frozen-lockfile.sh
- name: Install JS dependencies
run: pnpm install --frozen-lockfile

- name: Build WASM
run: yarn build-wasm
run: pnpm build-wasm

- name: Run tests
run: yarn test
run: pnpm test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ web/certs/

# Files produced by tests
*.avi

# We have switched to pnpm, ignore Yarn lockfile
yarn.lock
19 changes: 19 additions & 0 deletions .pnpmfile.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function afterAllResolved(lockfile, context) {
// The pnpm lockfile contains entries for all workspaces.
// This means that if the `e` repo is not cloned, we will get a different
// lockfile.
// The ugly workaround is to add an entry for e/web/teleport workspace manually.
// We pass an empty object because `e/web/teleport` package.json doesn't have
// any dependencies.
if (!lockfile.importers['e/web/teleport']) {
context.log(`Workspace 'e/web/teleport' is not present, patching lockfile.`);
lockfile.importers['e/web/teleport'] = {};
}
return lockfile;
}

module.exports = {
hooks: {
afterAllResolved,
},
};
4 changes: 2 additions & 2 deletions BUILD_macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ PRs with corrections and updates are welcome!
```

* To install tools for building the UI:
* `brew install node yarn`
* Currently, [`yarn`](https://classic.yarnpkg.com/en/docs/install) (< 2.0.0) is required
* `brew install node corepack`
* `corepack enable pnpm`
* The `Rust` and `Cargo` version in [build.assets/Makefile](https://github.com/gravitational/teleport/blob/master/build.assets/versions.mk#L11) (search for `RUST_VERSION`) are required.
* The [`wasm-pack`](https://github.com/rustwasm/wasm-pack) version in [build.assets/Makefile](https://github.com/gravitational/teleport/blob/master/build.assets/versions.mk#L12) (search for `WASM_PACK_VERSION`) is required:
`curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh`
Expand Down
39 changes: 31 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ TARBINS = $(addprefix teleport/,$(BINS))
CHECK_CARGO := $(shell cargo --version 2>/dev/null)
CHECK_RUST := $(shell rustc --version 2>/dev/null)

# Check if pnpm is enabled before using it
CHECK_PNPM := $(shell pnpm --version 2>/dev/null)
# Check if Corepack is installed before using it
CHECK_COREPACK := $(shell corepack --version 2>/dev/null)

RUST_TARGET_ARCH ?= $(CARGO_TARGET_$(OS)_$(ARCH))

CARGO_TARGET_darwin_amd64 := x86_64-apple-darwin
Expand Down Expand Up @@ -350,7 +355,7 @@ ifeq ("$(GITHUB_REPOSITORY_OWNER)","gravitational")
# This is done here to prevent any changes to the (BUI)LDFLAGS passed to the other binaries
TELEPORT_LDFLAGS ?= -ldflags '$(GO_LDFLAGS) -X github.com/gravitational/teleport/lib/modules.teleportBuildType=community'
endif
$(BUILDDIR)/teleport: ensure-webassets bpf-bytecode rdpclient
$(BUILDDIR)/teleport: ensure-js-package-manager ensure-webassets bpf-bytecode rdpclient
GOOS=$(OS) GOARCH=$(ARCH) $(CGOFLAG) go build -tags "webassets_embed $(PAM_TAG) $(FIPS_TAG) $(BPF_TAG) $(WEBASSETS_TAG) $(RDPCLIENT_TAG) $(PIV_BUILD_TAG) $(KUSTOMIZE_NO_DYNAMIC_PLUGIN)" -o $(BUILDDIR)/teleport $(BUILDFLAGS) $(TELEPORT_LDFLAGS) ./tool/teleport

# NOTE: Any changes to the `tsh` build here must be copied to `build.assets/windows/build.ps1`
Expand Down Expand Up @@ -678,16 +683,16 @@ release-windows: release-windows-unsigned
# Makefile. Linux uses the `teleterm` target in build.assets/Makefile.
#
# Either CONNECT_TSH_BIN_PATH or CONNECT_TSH_APP_PATH environment variable
# should be defined for the `yarn package-term` command to succeed. CI sets
# should be defined for the `pnpm package-term` command to succeed. CI sets
# this appropriately depending on whether a push build is running, or a
# proper release (a proper release needs the APP_PATH as that points to
# the complete signed package). See web/packages/teleterm/README.md for
# details.
.PHONY: release-connect
release-connect: | $(RELEASE_DIR)
yarn install --frozen-lockfile
yarn build-term
yarn package-term -c.extraMetadata.version=$(VERSION) --$(ELECTRON_BUILDER_ARCH)
pnpm install --frozen-lockfile
pnpm build-term
pnpm package-term -c.extraMetadata.version=$(VERSION) --$(ELECTRON_BUILDER_ARCH)
# Only copy proper builds with tsh.app to $(RELEASE_DIR)
# Drop -universal "arch" from dmg name when copying to $(RELEASE_DIR)
if [ -n "$$CONNECT_TSH_APP_PATH" ]; then \
Expand Down Expand Up @@ -1631,6 +1636,24 @@ ensure-webassets-e:
@if [[ "${WEBASSETS_SKIP_BUILD}" -eq 1 ]]; then mkdir -p webassets/teleport && mkdir -p webassets/e/teleport/app && cp web/packages/teleport/index.html webassets/e/teleport/index.html; \
else MAKE="$(MAKE)" "$(MAKE_DIR)/build.assets/build-webassets-if-changed.sh" Enterprise webassets/e/e-sha build-ui-e web e/web; fi

# Enables the pnpm package manager if building webassets is not skipped,
# pnpm is not already enabled and Corepack is available.
# We check if pnpm is enabled, as the user may not have permission to
# enable it, but it may already be available.
.PHONY: ensure-js-package-manager
ensure-js-package-manager:
ifneq ($(WEBASSETS_SKIP_BUILD),1)
@if [ -z "$(CHECK_PNPM)" ]; then \
if [ -n "$(CHECK_COREPACK)" ]; then \
echo 'Info: pnpm is not enabled via Corepack. Enabling pnpm...'; \
corepack enable pnpm; \
else \
echo 'Error: Corepack is not installed, cannot enable pnpm. See the installation guide https://pnpm.io/installation#using-corepack'; \
exit 1; \
fi \
fi
endif

.PHONY: init-submodules-e
init-submodules-e:
git submodule init e
Expand All @@ -1645,15 +1668,15 @@ backport:
.PHONY: ensure-js-deps
ensure-js-deps:
@if [[ "${WEBASSETS_SKIP_BUILD}" -eq 1 ]]; then mkdir -p webassets/teleport && touch webassets/teleport/index.html; \
else yarn install --ignore-scripts; fi
else pnpm install --frozen-lockfile; fi

.PHONY: build-ui
build-ui: ensure-js-deps
@[ "${WEBASSETS_SKIP_BUILD}" -eq 1 ] || yarn build-ui-oss
@[ "${WEBASSETS_SKIP_BUILD}" -eq 1 ] || pnpm build-ui-oss

.PHONY: build-ui-e
build-ui-e: ensure-js-deps
@[ "${WEBASSETS_SKIP_BUILD}" -eq 1 ] || yarn build-ui-e
@[ "${WEBASSETS_SKIP_BUILD}" -eq 1 ] || pnpm build-ui-e

.PHONY: docker-ui
docker-ui:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ Ensure you have installed correct versions of necessary dependencies:
* For `tsh` version > `10.x` with FIDO2 support, you will need `libfido2` and
`pkg-config` installed locally
* To build the web UI:
* [`yarn`](https://classic.yarnpkg.com/en/docs/install)(< 2.0.0) is required.
* If you prefer not to install/use yarn, but have docker available, you can run `make docker-ui` instead.
* [`pnpm`](https://pnpm.io/installation#using-corepack). If you have Node.js installed, run `corepack enable pnpm` to make `pnpm` available.
* If you prefer not to install/use pnpm, but have docker available, you can run `make docker-ui` instead.
* The `Rust` and `Cargo` version in [build.assets/Makefile](https://github.com/gravitational/teleport/blob/master/build.assets/versions.mk#L11) (search for `RUST_VERSION`) are required.
* The [`wasm-pack`](https://github.com/rustwasm/wasm-pack) version in [build.assets/Makefile](https://github.com/gravitational/teleport/blob/master/build.assets/versions.mk#L12) (search for `WASM_PACK_VERSION`) is required.
* [`binaryen`](https://github.com/WebAssembly/binaryen) (which contains `wasm-opt`) is required to be installed manually
Expand Down
2 changes: 1 addition & 1 deletion build.assets/Dockerfile-arm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ RUN NODE_ARCH="$(if [ "$BUILDARCH" = 'amd64' ]; then echo 'x64'; else echo 'arm6
curl -o "$NODE_FILE" -fsSL "$NODE_URL" && \
tar -xJf "$NODE_FILE" -C /usr/local/lib/nodejs-linux --strip-components=1 && \
rm -f "$NODE_FILE"
RUN corepack enable yarn
RUN corepack enable pnpm

# Install Go.
ARG GOLANG_VERSION
Expand Down
2 changes: 2 additions & 0 deletions build.assets/Dockerfile-node
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ FROM node:${NODE_VERSION}-buster AS buildbox
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
ARG BUILDARCH

RUN corepack enable pnpm

COPY locale.gen /etc/locale.gen
COPY profile /etc/profile
ENV LANGUAGE="en_US.UTF-8" \
Expand Down
6 changes: 3 additions & 3 deletions build.assets/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,16 @@ buildbox-node:
.PHONY:teleterm
teleterm: buildbox-node
docker run $(DOCKERFLAGS) $(NOROOT) $(BUILDBOX_NODE) \
bash -c "cd $(SRCDIR) && export CONNECT_TSH_BIN_PATH=\$$PWD/../teleport/build/tsh && yarn install --frozen-lockfile && yarn build-term && yarn package-term -c.extraMetadata.version=$(CONNECT_VERSION)"
bash -c "cd $(SRCDIR) && export CONNECT_TSH_BIN_PATH=\$$PWD/../teleport/build/tsh && pnpm install --frozen-lockfile && pnpm build-term && pnpm package-term -c.extraMetadata.version=$(CONNECT_VERSION)"

# Builds webassets inside Docker.
.PHONY:ui
ui: buildbox
docker run -u $(UID):$(GID) -v "$$(pwd)/../":/teleport $(BUILDBOX) \
bash -c "cd ../teleport && yarn install --frozen-lockfile && yarn build-ui"
bash -c "cd ../teleport && pnpm install --frozen-lockfile && pnpm build-ui"

# Builds webassets inside the buildbox-node container via the
# Makefile (instead of calling yarn directly as the ui target above does)
# Makefile (instead of calling pnpm directly as the ui target above does)
# as that computes a SHA to prevent unnecessary rebuilds. This is used by
# the CentOS 7 build that does not have a new enough version of node.js
# to build webassets. Enterprise webassets will only be built if the e
Expand Down
10 changes: 5 additions & 5 deletions build.assets/build-webassets-if-changed.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ for i in "${!SRC_DIRECTORIES[@]}"; do
SRC_DIRECTORIES[i]="$ROOT_PATH/${SRC_DIRECTORIES[i]}"
done

# Calculate the current hash-of-hashes of the given source directories, package.json, and yarn.lock.
# We exclude node_modules as it's covered by package.json and yarn.lock.
# Calculate the current hash-of-hashes of the given source directories, package.json, and pnpm-lock.yaml.
# We exclude node_modules as it's covered by package.json and pnpm-lock.yaml.
# We also exclude .swc as it's a cache directory for the swc compiler,
# and ironrdp/pkg as it's filled with the generated wasm files.
function calculate_sha() {
#shellcheck disable=SC2086
#We want to split $SHASUM on spaces so we dont want it quoted.
find "${SRC_DIRECTORIES[@]}" "$ROOT_PATH/package.json" "$ROOT_PATH/yarn.lock" \
find "${SRC_DIRECTORIES[@]}" "$ROOT_PATH/package.json" "$ROOT_PATH/pnpm-lock.yaml" \
-not \( -type d \( -name node_modules -o -name .swc -o -path '*ironrdp/pkg*' \) -prune \) \
-type f -print0 | \
LC_ALL=C sort -z | xargs -0 $SHASUM | awk '{print $1}' | $SHASUM | tr -d ' -'
Expand All @@ -86,10 +86,10 @@ if [ "$BUILD" = "true" ]; then \
print_for_user "detected changes in $TYPE webassets. Rebuilding..."
"$MAKE" -C "$ROOT_PATH" "$BUILD_TARGET"; \
# Recalculate the current SHA and record into the LAST_SHA_FILE. The make target is expected to have
# created any necessary directories here. The recalculation is necessary as yarn.lock may have been
# created any necessary directories here. The recalculation is necessary as pnpm-lock.yaml may have been
# updated by the build process.
mkdir -p "$(dirname "$LAST_SHA_FILE")"
# Save SHA with yarn.lock before yarn install
# Save SHA with pnpm-lock.yaml before installing dependencies.
echo "$CURRENT_SHA" > "$LAST_SHA_FILE"
print_for_user "$TYPE webassets successfully updated."
else
Expand Down
8 changes: 4 additions & 4 deletions build.assets/windows/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function Install-Node {
Expand-Archive -Path $NodeZipfile -DestinationPath $ToolchainDir
Rename-Item -Path "$ToolchainDir/node-v$NodeVersion-win-x64" -NewName "$ToolchainDir/node"
Enable-Node -ToolchainDir $ToolchainDir
corepack enable yarn
corepack enable pnpm
Write-Host "::endgroup::"
}
}
Expand Down Expand Up @@ -440,9 +440,9 @@ function Build-Connect {
$CommandDuration = Measure-Block {
Write-Host "::group::Building Teleport Connect..."
$env:CONNECT_TSH_BIN_PATH = "$SignedTshBinaryPath"
yarn install --frozen-lockfile
yarn build-term
yarn package-term "-c.extraMetadata.version=$TeleportVersion"
pnpm install --frozen-lockfile
pnpm build-term
pnpm package-term "-c.extraMetadata.version=$TeleportVersion"
$BinaryName = "Teleport Connect Setup-$TeleportVersion.exe"
Invoke-SignBinary -UnsignedBinaryPath "$TeleportSourceDirectory\web\packages\teleterm\build\release\$BinaryName" `
-SignedBinaryPath "$ArtifactDirectory\$BinaryName"
Expand Down
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const esModules = [
'd3-color',
'd3-scale',
'd3-interpolate',
'd3-time-format',
'd3-array',
'd3-format',
'd3-time',
'd3-shape',
'd3-path',
'internmap',
'@nivo/bar',
].join('|');

/** @type {import('@jest/types').Config.InitialOptions} */
Expand All @@ -27,7 +29,12 @@ module.exports = {
// '**/packages/design/src/**/*.jsx',
'**/packages/shared/components/**/*.jsx',
],
transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
// Copied from https://jestjs.io/docs/configuration#transformignorepatterns-arraystring
// Because in pnpm packages are symlinked to node_modules/.pnpm,
// we need to transform packages in that directory.
// We use a relative pattern to match the second 'node_modules/' in
// 'node_modules/.pnpm/@[email protected]/node_modules/@scope/pkg-b/'.
transformIgnorePatterns: [`node_modules/(?!.pnpm|${esModules})`],
coverageReporters: ['text-summary', 'lcov'],
testPathIgnorePatterns: ['e2e'],
setupFilesAfterEnv: [
Expand Down
Loading

0 comments on commit 620c0a0

Please sign in to comment.