Skip to content

Commit 79fd756

Browse files
committed
fix: prevent race condition when using commit rules in conjunction
This commit addresses errors when attempting to use `make commit`. Namely, when using `make commit`, we would rename `tsconfig.json` to prevent commitlint from attempting to compile TypeScript; however, the various commitlint recipes were assuming that `tsconfig.json` exists. This commit avoids this race condition by adding conditional logic for handling `tsconfig.json`.
1 parent b1825cb commit 79fd756

File tree

2 files changed

+79
-28
lines changed

2 files changed

+79
-28
lines changed

tools/make/lib/git/commitizen.mk

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ COMMITIZEN ?= $(BIN_DIR)/cz
3333
# RULES #
3434

3535
#/
36-
# Provides an interactive prompt for entering commit message information in accordance with project Git commit conventions.
36+
# Performs initialization tasks.
3737
#
3838
# ## Notes
3939
#
@@ -42,12 +42,45 @@ COMMITIZEN ?= $(BIN_DIR)/cz
4242
# @private
4343
#
4444
# @example
45-
# make commitizen-commit
45+
# make commitlint-init
4646
#/
47-
commitizen-commit: $(NODE_MODULES) $(COMMITIZEN)
47+
commitizen-init:
48+
ifneq ("$(wildcard $(ROOT_DIR)/tsconfig.json)", "")
4849
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
49-
$(QUIET) "$(COMMITIZEN)" || (mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1)
50+
endif
51+
52+
.PHONY: commitizen-init
53+
54+
#/
55+
# Performs clean-up tasks.
56+
#
57+
# @private
58+
#
59+
# @example
60+
# make commitizen-cleanup
61+
#/
62+
commitizen-cleanup:
63+
ifneq ("$(wildcard $(ROOT_DIR)/tsconfig.json.tmp)", "")
5064
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
65+
endif
66+
67+
.PHONY: commitizen-cleanup
68+
69+
#/
70+
# Provides an interactive prompt for entering commit message information in accordance with project Git commit conventions.
71+
#
72+
# ## Notes
73+
#
74+
# - We have to temporarily move the `tsconfig` file, as `commitlint` (erroneously) attempts to use the file for compiling TypeScript.
75+
#
76+
# @private
77+
#
78+
# @example
79+
# make commitizen-commit
80+
#/
81+
commitizen-commit: $(NODE_MODULES) $(COMMITIZEN) commitizen-init
82+
$(QUIET) "$(COMMITIZEN)" || ($(MAKE) -f $(this_file) commitizen-cleanup && exit 1)
83+
$(QUIET) $(MAKE) -f $(this_file) commitizen-cleanup
5184

5285
.PHONY: commitizen-commit
5386

@@ -63,9 +96,8 @@ commitizen-commit: $(NODE_MODULES) $(COMMITIZEN)
6396
# @example
6497
# make commitizen-retry-commit
6598
#/
66-
commitizen-retry-commit: $(NODE_MODULES) $(COMMITIZEN)
67-
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
68-
$(QUIET) "$(COMMITIZEN)" --retry || (mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1)
69-
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
99+
commitizen-retry-commit: $(NODE_MODULES) $(COMMITIZEN) commitizen-init
100+
$(QUIET) "$(COMMITIZEN)" --retry || ($(MAKE) -f $(this_file) commitizen-cleanup && exit 1)
101+
$(QUIET) $(MAKE) -f $(this_file) commitizen-cleanup
70102

71103
.PHONY: commitizen-retry-commit

tools/make/lib/lint/git/commitlint.mk

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ COMMITLINT_FLAGS ?= \
3939
# RULES #
4040

4141
#/
42-
# Lints a commit.
42+
# Performs linting initialization tasks.
4343
#
4444
# ## Notes
4545
#
@@ -48,41 +48,61 @@ COMMITLINT_FLAGS ?= \
4848
# @private
4949
#
5050
# @example
51-
# make commitlint
51+
# make commitlint-init
5252
#/
53-
commitlint: $(NODE_MODULES)
53+
commitlint-init:
54+
ifneq ("$(wildcard $(ROOT_DIR)/tsconfig.json)", "")
5455
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
55-
$(QUIET) "$(COMMITLINT)" $(COMMITLINT_FLAGS) --edit || ( mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1 )
56+
endif
57+
58+
.PHONY: commitlint-init
59+
60+
#/
61+
# Performs linting clean-up tasks.
62+
#
63+
# @private
64+
#
65+
# @example
66+
# make commitlint-cleanup
67+
#/
68+
commitlint-cleanup:
69+
ifneq ("$(wildcard $(ROOT_DIR)/tsconfig.json.tmp)", "")
5670
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
71+
endif
5772

58-
.PHONY: commitlint
73+
.PHONY: commitlint-cleanup
5974

6075
#/
61-
# Lints a commit message.
76+
# Lints a commit.
6277
#
63-
# ## Notes
78+
# @private
6479
#
65-
# - We have to temporarily move the `tsconfig` file, as `commitlint` (erroneously) attempts to use the file for compiling TypeScript.
80+
# @example
81+
# make commitlint
82+
#/
83+
commitlint: $(NODE_MODULES) commitlint-init
84+
$(QUIET) "$(COMMITLINT)" $(COMMITLINT_FLAGS) --edit || ( $(MAKE) -f $(this_file) commitlint-cleanup && exit 1 )
85+
$(QUIET) $(MAKE) -f $(this_file) commitlint-cleanup
86+
87+
.PHONY: commitlint
88+
89+
#/
90+
# Lints a commit message.
6691
#
6792
# @private
6893
#
6994
# @example
7095
# make commitlint-message
7196
#/
72-
commitlint-message: $(NODE_MODULES)
73-
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
74-
$(QUIET) ( printf "$(GIT_COMMIT_MESSAGE)" | "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || ( mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1 )
75-
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
97+
commitlint-message: $(NODE_MODULES) commitlint-init
98+
$(QUIET) ( printf "$(GIT_COMMIT_MESSAGE)" | "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || ( $(MAKE) -f $(this_file) commitlint-cleanup && exit 1 )
99+
$(QUIET) $(MAKE) -f $(this_file) commitlint-cleanup
76100

77101
.PHONY: commitlint-message
78102

79103
#/
80104
# Lints a list of files, each containing a commit message.
81105
#
82-
# ## Notes
83-
#
84-
# - We have to temporarily move the `tsconfig` file, as `commitlint` (erroneously) attempts to use the file for compiling TypeScript.
85-
#
86106
# @private
87107
#
88108
# @param {string} FILES - list of file paths
@@ -91,13 +111,12 @@ commitlint-message: $(NODE_MODULES)
91111
# @example
92112
# make commitlint-files FILES='/foo/commit.txt'
93113
#/
94-
commitlint-files: $(NODE_MODULES)
95-
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
114+
commitlint-files: $(NODE_MODULES) commitlint-init
96115
ifeq ($(FAIL_FAST), true)
97116
$(QUIET) for file in $(FILES); do \
98117
echo ''; \
99118
echo "Linting commit message:"; \
100-
( cat $$file | grep -v '^#' | "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || ( mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1 ); \
119+
( cat $$file | grep -v '^#' | "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || ( $(MAKE) -f $(this_file) commitlint-cleanup && exit 1 ); \
101120
done
102121
else
103122
$(QUIET) for file in $(FILES); do \
@@ -106,6 +125,6 @@ else
106125
( cat $$file | grep -v '^#' | "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || echo 'Linting failed.'; \
107126
done
108127
endif
109-
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
128+
$(QUIET) $(MAKE) -f $(this_file) commitlint-cleanup
110129

111130
.PHONY: commitlint-files

0 commit comments

Comments
 (0)