Skip to content

Commit

Permalink
change semver linting rules to new guidance to never pin semconv dep …
Browse files Browse the repository at this point in the history
…or use incubating
  • Loading branch information
trentm committed Jan 17, 2025
1 parent f84cced commit c1f5462
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions scripts/lint-semconv-deps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ const TOP = path.resolve(fileURLToPath(new URL('.', import.meta.url)), '..');
const SEMCONV = '@opentelemetry/semantic-conventions';
const USE_COLOR = process.stdout.isTTY && !process.env.NO_COLOR?.length > 0;

let numProbs = 0;
function problem(...args) {
numProbs += 1;
if (USE_COLOR) {
process.stdout.write('\x1b[31m');
}
args.unshift('lint-semconv-deps error:')
args.unshift('lint-semconv-deps error:');
console.log(...args);
if (USE_COLOR) {
process.stdout.write('\x1b[39m');
Expand All @@ -56,53 +58,41 @@ function getAllWorkspaceDirs() {
}

function lintSemconvDeps() {
let numProbs = 0;
const wsDirs = getAllWorkspaceDirs();

for (let wsDir of wsDirs) {
const pj = JSON.parse(
fs.readFileSync(path.join(wsDir, 'package.json'), 'utf8')
);
const depRange = pj?.dependencies?.[SEMCONV];
if (!depRange) {
const devDepRange = pj?.devDependencies?.[SEMCONV];
if (!(depRange || devDepRange)) {
continue;
}

// Is incubating entry-point in use?
// Rule: The semconv dep should *not* be pinned. Expect `^X.Y.Z`.
const pinnedVerRe = /^\d+\.\d+\.\d+$/;
if (depRange && pinnedVerRe.exec(depRange)) {
problem(`${wsDir}/package.json: package ${pj.name} pins "${SEMCONV}" in dependencies, but should not (see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#why-not-pin-the-version)`);
} else if (devDepRange && pinnedVerRe.exec(devDepRange)) {
problem(`${wsDir}/package.json: package ${pj.name} pins "${SEMCONV}" in devDependencies, but should not (see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#why-not-pin-the-version)`);
}

// Rule: The incubating entry-point should not be used.
const srcFiles = globSync(path.join(wsDir, 'src', '**', '*.ts'));
let usesIncubating = false;
const usesIncubatingRe = /import \{?[^\{]* from '@opentelemetry\/semantic-conventions\/incubating'/s;
const usesIncubatingRe = /import\s+\{?[^{;]*\s+from\s+'@opentelemetry\/semantic-conventions\/incubating'/s;
for (let srcFile of srcFiles) {
const srcText = fs.readFileSync(srcFile, 'utf8');
const match = usesIncubatingRe.exec(srcText);
if (match) {
usesIncubating = true;
break;
}
}

// Rule: If the semconv "incubating" entry-point is used, then the dep
// should be pinned. Otherwise it should not be pinned.
const pinnedVerRe = /^\d+\.\d+\.\d+$/;
const pins = Boolean(pinnedVerRe.exec(depRange));
if (usesIncubating) {
if (!pins) {
problem(`package ${pj.name} (in ${wsDir}) imports "${SEMCONV}/incubating" but does not *pin* the dependency: \`"${SEMCONV}": "${depRange}"\``);
numProbs += 1;
}
} else {
if (pins) {
problem(`package ${pj.name} (in ${wsDir}) does not import "${SEMCONV}/incubating" but pins the dependency: \`"${SEMCONV}": "${depRange}"\` (it could use a caret-range)`);
numProbs += 1;
problem(`${srcFile}: uses the 'incubating' entry-point from '@opentelemetry/semantic-conventions', but should not (see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv)`)
}
}
}

return numProbs;
}

// mainline
const numProbs = await lintSemconvDeps();
await lintSemconvDeps();
if (numProbs > 0) {
process.exitCode = 1;
}
Expand Down

0 comments on commit c1f5462

Please sign in to comment.