SonarSource/sonarqube · error · IllegalArgumentException
Failed to set the New Code Definition. The given value is no
Error message
Failed to set the New Code Definition. The given value is not compatible with the Clean as You Code methodology. Please refer to the documentation for compliant options.
What it means
After writing the requested New Code Period value into the DTO, SetAction verifies it against CaycUtils.isNewCodePeriodCompliant and throws IllegalArgumentException if the combination violates the Clean as You Code methodology (e.g. certain date/reference-branch settings that are not CaYC-compliant). The upsert is aborted before commit, so the invalid definition is never persisted.
Source
Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java:178
if (branchKey != null) {
branch = getBranch(dbSession, project, branchKey);
dto.setBranchUuid(branch.getUuid());
} else if (isCommunityEdition) {
// in CE set main branch value instead of project value
branch = getMainBranch(dbSession, project);
dto.setBranchUuid(branch.getUuid());
}
dto.setProjectUuid(project.getUuid());
} else {
userSession.checkIsSystemAdministrator();
}
setValue(dbSession, dto, type, project, branch, valueStr);
if (!CaycUtils.isNewCodePeriodCompliant(dto.getType(), dto.getValue())) {
throw new IllegalArgumentException("Failed to set the New Code Definition. The given value is not compatible with the Clean as " +
"You Code methodology. Please refer to the documentation for compliant options.");
}
newCodePeriodDao.upsert(dbSession, dto);
dbSession.commit();
}
}
private void setValue(DbSession dbSession, NewCodePeriodDto dto, NewCodePeriodType type, @Nullable ProjectDto project,
@Nullable BranchDto branch, @Nullable String value) {
switch (type) {
case PREVIOUS_VERSION -> Preconditions.checkArgument(value == null, "Unexpected value for type '%s'", type);
case NUMBER_OF_DAYS -> {
requireValue(type, value);
dto.setValue(parseDays(value));
}
case SPECIFIC_ANALYSIS -> {
checkValuesForSpecificBranch(type, value, project, branch);View on GitHub (pinned to 184c821202)
Solutions
- Choose a CaYC-compliant option per the linked documentation, typically PREVIOUS_VERSION or NUMBER_OF_DAYS for the project/overall scope.
- If a legacy date-based definition is needed, set it at branch level only where permitted.
- Check CaycUtils/edition constraints to confirm which types are valid for your scope (overall vs project vs branch).
Example fix
// before type=SPECIFIC_ANALYSIS&value=2023-01-01 // after type=PREVIOUS_VERSION
Defensive patterns
Strategy: validation
Validate before calling
const CAYC_COMPLIANT = ['PREVIOUS_VERSION', 'NUMBER_OF_DAYS'];
if (!CAYC_COMPLIANT.includes(params.type)) throw new Error('Type not CaYC-compliant: ' + params.type); Try / catch
try { ... } catch (e) { if (String(e.message).includes('Clean as You Code')) { log('Non-compliant New Code Definition rejected; use PREVIOUS_VERSION'); } throw e; } Prevention
- Default to PREVIOUS_VERSION unless a compliant alternative is explicitly required.
- Review the Clean as You Code docs when migrating legacy date-based definitions.
When it happens
Trigger: Calling api/new_code_periods/set with a type/value pair that parses successfully but fails the CaYC compliance check — e.g. a SPECIFIC_ANALYSIS date for the overall setting or a reference-branch configuration disallowed in that scope/edition.
Common situations: Migrating legacy 'days' or fixed-date definitions into newer SonarQube versions that enforce CaYC; administrators trying to set an overall definition to a type only valid on branches; edition downgrades changing which types are compliant.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- Invalid type: %s
- Transition '%s' not supported. Only %s are supported.
- If branch key is specified, project key needs to be specifie
- Failed to parse number of days: %s
- Invalid type '%s'. %s can only be set with types: %s
AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09).
Data as JSON: /api/errors/35ef89027b47c370.
Report an issue: GitHub.