|
1 |
| -import { mkdirSync, writeFileSync } from "fs"; |
| 1 | +import { existsSync, mkdirSync, writeFileSync } from "fs"; |
2 | 2 | import path from "path";
|
3 | 3 |
|
4 | 4 | import { Config } from "./config-utils";
|
@@ -87,23 +87,31 @@ export function addDiagnostic(
|
87 | 87 | diagnostic: DiagnosticMessage,
|
88 | 88 | ) {
|
89 | 89 | const logger = getActionsLogger();
|
| 90 | + const databasePath = getCodeQLDatabasePath(config, language); |
90 | 91 | const diagnosticsPath = path.resolve(
|
91 |
| - getCodeQLDatabasePath(config, language), |
| 92 | + databasePath, |
92 | 93 | "diagnostic",
|
93 | 94 | "codeql-action",
|
94 | 95 | );
|
95 | 96 |
|
96 |
| - try { |
97 |
| - // Create the directory if it doesn't exist yet. |
98 |
| - mkdirSync(diagnosticsPath, { recursive: true }); |
| 97 | + // Check that the database exists before writing to it. |
| 98 | + if (existsSync(databasePath)) { |
| 99 | + try { |
| 100 | + // Create the directory if it doesn't exist yet. |
| 101 | + mkdirSync(diagnosticsPath, { recursive: true }); |
99 | 102 |
|
100 |
| - const jsonPath = path.resolve( |
101 |
| - diagnosticsPath, |
102 |
| - `codeql-action-${diagnostic.timestamp}.json`, |
103 |
| - ); |
| 103 | + const jsonPath = path.resolve( |
| 104 | + diagnosticsPath, |
| 105 | + `codeql-action-${diagnostic.timestamp}.json`, |
| 106 | + ); |
104 | 107 |
|
105 |
| - writeFileSync(jsonPath, JSON.stringify(diagnostic)); |
106 |
| - } catch (err) { |
107 |
| - logger.warning(`Unable to write diagnostic message to database: ${err}`); |
| 108 | + writeFileSync(jsonPath, JSON.stringify(diagnostic)); |
| 109 | + } catch (err) { |
| 110 | + logger.warning(`Unable to write diagnostic message to database: ${err}`); |
| 111 | + } |
| 112 | + } else { |
| 113 | + logger.info( |
| 114 | + `Writing a diagnostic for ${language}, but the database at ${databasePath} does not exist yet.`, |
| 115 | + ); |
108 | 116 | }
|
109 | 117 | }
|
0 commit comments