Skip to content

Commit 481814e

Browse files
authored
fix: install wrapper after version/cache steps (#357)
- Extract version detection into getInstalledVersion function - Move wrapper installation after version detection and caching - Ensures version output is captured before wrapper intercepts stdout - Prevents caching wrapped binary, which caused issues on cache hits - Add test to verify tflint-version output with wrapper enabled Fixes #356
1 parent acd1575 commit 481814e

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ jobs:
164164
steps:
165165
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
166166
- name: Use Action
167+
id: setup-tflint
167168
uses: ./
168169
with:
169170
tflint_version: ${{ matrix.tflint_version }}
@@ -180,6 +181,14 @@ jobs:
180181
else
181182
echo "TFLint Exit Code captured."
182183
fi
184+
- name: Verify Version Output
185+
run: |
186+
if [[ -z "${{ steps.setup-tflint.outputs.tflint-version }}" ]]; then
187+
echo "TFLint version output not captured."
188+
exit 1
189+
else
190+
echo "TFLint version captured: ${{ steps.setup-tflint.outputs.tflint-version }}"
191+
fi
183192
184193
integration-cache:
185194
name: 'Integration test: cache'

dist/index.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89055,6 +89055,30 @@ async function downloadCLI(url, checksums, version) {
8905589055
return pathToCLI;
8905689056
}
8905789057

89058+
async function getInstalledVersion() {
89059+
try {
89060+
let stdout = '';
89061+
await exec.exec('tflint', ['--version'], {
89062+
listeners: {
89063+
stdout: (data) => {
89064+
stdout += data.toString();
89065+
},
89066+
},
89067+
});
89068+
89069+
const firstLine = stdout.split('\n')[0];
89070+
const match = firstLine.match(/TFLint version (.+)/);
89071+
if (match) {
89072+
const installedVersion = match[1];
89073+
core.setOutput('tflint-version', installedVersion);
89074+
} else {
89075+
core.warning('Unable to parse tflint version from output');
89076+
}
89077+
} catch (error) {
89078+
core.warning(`Failed to get tflint version: ${error.message}`);
89079+
}
89080+
}
89081+
8905889082
async function installWrapper(pathToCLI) {
8905989083
// Move the original tflint binary to a new location
8906089084
await io.mv(external_path_.join(pathToCLI, 'tflint'), external_path_.join(pathToCLI, 'tflint-bin'));
@@ -89096,11 +89120,6 @@ async function run() {
8909689120

8909789121
pathToCLI = await downloadCLI(url, checksums, version);
8909889122

89099-
if (wrapper) {
89100-
await installWrapper(pathToCLI);
89101-
}
89102-
89103-
// Cache the tool for future runs
8910489123
core.info('Adding to the tool cache...');
8910589124
pathToCLI = await tool_cache.cacheDir(pathToCLI, 'tflint', normalizedVersion, arch);
8910689125
core.info(`Successfully cached TFLint to ${pathToCLI}`);
@@ -89111,27 +89130,11 @@ async function run() {
8911189130
const matchersPath = __nccwpck_require__.ab + "matchers.json";
8911289131
core.info(`##[add-matcher]${matchersPath}`);
8911389132

89114-
// Get and output actual installed version
89115-
try {
89116-
let stdout = '';
89117-
await exec.exec('tflint', ['--version'], {
89118-
listeners: {
89119-
stdout: (data) => {
89120-
stdout += data.toString();
89121-
},
89122-
},
89123-
});
89133+
await getInstalledVersion();
8912489134

89125-
const firstLine = stdout.split('\n')[0];
89126-
const match = firstLine.match(/TFLint version (.+)/);
89127-
if (match) {
89128-
const installedVersion = match[1];
89129-
core.setOutput('tflint-version', installedVersion);
89130-
} else {
89131-
core.warning('Unable to parse tflint version from output');
89132-
}
89133-
} catch (error) {
89134-
core.warning(`Failed to get tflint version: ${error.message}`);
89135+
// Must happen after version detection and caching, which depend on the real binary
89136+
if (wrapper) {
89137+
await installWrapper(pathToCLI);
8913589138
}
8913689139

8913789140
return version;

src/setup-tflint.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ async function downloadCLI(url, checksums, version) {
104104
return pathToCLI;
105105
}
106106

107+
async function getInstalledVersion() {
108+
try {
109+
let stdout = '';
110+
await exec.exec('tflint', ['--version'], {
111+
listeners: {
112+
stdout: (data) => {
113+
stdout += data.toString();
114+
},
115+
},
116+
});
117+
118+
const firstLine = stdout.split('\n')[0];
119+
const match = firstLine.match(/TFLint version (.+)/);
120+
if (match) {
121+
const installedVersion = match[1];
122+
core.setOutput('tflint-version', installedVersion);
123+
} else {
124+
core.warning('Unable to parse tflint version from output');
125+
}
126+
} catch (error) {
127+
core.warning(`Failed to get tflint version: ${error.message}`);
128+
}
129+
}
130+
107131
async function installWrapper(pathToCLI) {
108132
// Move the original tflint binary to a new location
109133
await io.mv(path.join(pathToCLI, 'tflint'), path.join(pathToCLI, 'tflint-bin'));
@@ -145,11 +169,6 @@ async function run() {
145169

146170
pathToCLI = await downloadCLI(url, checksums, version);
147171

148-
if (wrapper) {
149-
await installWrapper(pathToCLI);
150-
}
151-
152-
// Cache the tool for future runs
153172
core.info('Adding to the tool cache...');
154173
pathToCLI = await tc.cacheDir(pathToCLI, 'tflint', normalizedVersion, arch);
155174
core.info(`Successfully cached TFLint to ${pathToCLI}`);
@@ -160,27 +179,11 @@ async function run() {
160179
const matchersPath = path.join(__dirname, '..', '.github', 'matchers.json');
161180
core.info(`##[add-matcher]${matchersPath}`);
162181

163-
// Get and output actual installed version
164-
try {
165-
let stdout = '';
166-
await exec.exec('tflint', ['--version'], {
167-
listeners: {
168-
stdout: (data) => {
169-
stdout += data.toString();
170-
},
171-
},
172-
});
173-
174-
const firstLine = stdout.split('\n')[0];
175-
const match = firstLine.match(/TFLint version (.+)/);
176-
if (match) {
177-
const installedVersion = match[1];
178-
core.setOutput('tflint-version', installedVersion);
179-
} else {
180-
core.warning('Unable to parse tflint version from output');
181-
}
182-
} catch (error) {
183-
core.warning(`Failed to get tflint version: ${error.message}`);
182+
await getInstalledVersion();
183+
184+
// Must happen after version detection and caching, which depend on the real binary
185+
if (wrapper) {
186+
await installWrapper(pathToCLI);
184187
}
185188

186189
return version;

0 commit comments

Comments
 (0)