Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation errors in vscode, but not in maven nor in Intellij #1247

Open
yusijs opened this issue Oct 25, 2022 · 10 comments
Open

Compilation errors in vscode, but not in maven nor in Intellij #1247

yusijs opened this issue Oct 25, 2022 · 10 comments
Assignees

Comments

@yusijs
Copy link

yusijs commented Oct 25, 2022

(Quick note: I'm not 100% if this is the right repo for this, as I'm not 100% sure if the error is here or with the language server (or something else entirely), but trying here first)

Brief backstory: My colleagues use Intellij as they work fulltime on backend and prefer it. I primarily work frontend, and use Vscode. This has worked fine for a while, but recently (1-2 months back) it started throwing weird errors.

When I open the project, I get several of the error messages below in the "Problems" tab. If I try running my debugger and click "Proceed" in the prompt about build errors, it fails to start up with this error:

Caused by: java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from Class<DocumentValidator> to Class<? extends ConstraintValidator<?,?>>

	at com.equinor.gom.model.validator.annotation.ValidateDocument.<clinit>(ValidateDocument.java:14)
ValidateDocument.java:14
	at java.base/java.lang.Class.forName0(Native Method)

If I close java files and run mvn clean install in the terminal, then run the debugger, I get the prompt about build errors, but after clicking "Proceed" the application starts up fine. If I run the application with mvn spring-boot:run, it also starts fine.

Relevant code in stacktrace:
@Constraint(validatedBy = {AssetAreaValidator.class})

AssetAreaValidator:

@Component
@Slf4j
public class AssetAreaValidator extends parentValidator implements ConstraintValidator<ValidateAssetArea, AssetArea> {

    @Override
    public void initialize(ValidateAssetArea constraintAnnotation) {
        super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(AssetArea value, ConstraintValidatorContext context) {
       return validate(value, context);
    }

    private boolean validate(AssetArea value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }

        // create factory in order to generate correct validators
        var validatorFactory = new VerifierFactory<Container>();

        return verifierOrchestrator.validateAllRulesForSingleObject(value, validatorFactory,
                VerifierOrchestrator.DocumentEntity.assetArea, context);
    }
}

parentValidator:

public abstract class parentValidator<T> {
    protected ValidatorUtil validatorUtil = null;
    protected VerifierOrchestrator verifierOrchestrator = null;

    protected void initialize(T constraintAnnotation) {
        sanityCheck();
    }

    private void sanityCheck() {
        if (validatorUtil == null) {
            validatorUtil = ApplicationContextProvider.getBean(ValidatorUtil.class);
        }
        if (verifierOrchestrator == null) {
            verifierOrchestrator = ApplicationContextProvider.getBean(VerifierOrchestrator.class);
        }
    }
}
Environment
  • Operating System: macOS Ventura
  • JDK version: 11.0.16.1 (tried with 11.0.6 as well)
  • Visual Studio Code version: 1.72.2 (Universal)
  • Java extension version: Pre-release version
  • Java Debugger extension version: Pre-release version
{
  message: '[Error - 2:53:37 PM] 25. okt. 2022, 14:53:37 Error occured while building workspace. Details: \n' +
    ' message: Type mismatch: cannot convert from Class<AssetAreaValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateAssetArea.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<AssetLocationValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateAssetLocation.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<ContainerValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateContainer.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<ContractualRateValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateContractualRate.java; line: 13\n' +
    ' message: Type mismatch: cannot convert from Class<CurrencyValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateCurrency.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<DocumentValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateDocument.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<DocumentBalanceValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateDocumentBalance.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<DocumentCategoryValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateDocumentCategory.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<DocumentMovementValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateDocumentMovement.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<IcsControlInvoiceLineValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateIcsIcsControlInvoiceLine.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<InvoiceValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateInvoice.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<InvoiceLineValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateInvoiceLine.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<LabelValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateLabel.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<LegalEntityValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateLegalEntity.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<LineTypeValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateLineType.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<MovementTypeValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateMovementType.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<PipelineBalanceValidaor> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidatePipelineBalance.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<PipelineNodeValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidatePipelineNode.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<PipelineSystemValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidatePipelineSystem.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<QuantityBalanceValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateQuantityBalance.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<QuantityMovementValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateQuantityMovement.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<RateChangeTriggerValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateRateChangeTrigger.java; line: 15\n' +
    ' message: Type mismatch: cannot convert from Class<TemplateValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateTemplate.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<UomValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateUom.java; line: 14\n' +
    ' message: Type mismatch: cannot convert from Class<ValueMappingValidator> to Class<? extends ConstraintValidator<?,?>>; code: 16777233; resource: /Users/RHENRI/dev/oilmod-gom/backend/model/src/main/java/com/equinor/gom/model/validator/annotation/ValidateValueMapping.java; line: 14',
  level: 'info',
  timestamp: '2022-10-25 14:53:37.382'
}
Current Result

VS Code fails to compile the application, resulting in a runtime error, but running maven directly works fine, and debugger also works after I run mvn clean install

Expected Result

VS Code should just work ™️

Additional Informations

launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Launch GomApplication",
            "request": "launch",
            "mainClass": "com.equinor.gom.backend.GomApplication",
            "console": "internalConsole",
            "projectName": "backend",
            "args": [
                "--spring.profiles.active=local",
                "--database=${command:SpecifyProgramArgs}"
            ]
        }
    ]
}
@testforstephen
Copy link
Contributor

@yusijs Thanks for reaching out. AFAIK, a new feature nullable annotation analysis recently introduced by upstream language server may break the compilation of normal annotations in some cases, not sure if your project is related to this.

Could you try to disable the nullable annotation feature first by setting the following user settings to empty?

    "java.compile.nullAnalysis.nonnull": [
    ],
    "java.compile.nullAnalysis.nullable": [
    ]

@yusijs
Copy link
Author

yusijs commented Oct 26, 2022

Good grief. I actually looked at those, but decided it was probably not related. After trying it out - yes, yes it was. The errors are now gone with the wind, along with my burning hatred for the java compiler :D

Thanks for your help @testforstephen - perhaps this should be documented somewhere in the readme? Or would java developers actually understand the relationship here? :)

@testforstephen
Copy link
Contributor

Here is a new regression issue with "null annotation analysis".

cc:// @rgrunber @CsCherrYY

@testforstephen
Copy link
Contributor

Good grief. I actually looked at those, but decided it was probably not related. After trying it out - yes, yes it was. The errors are now gone with the wind, along with my burning hatred for the java compiler :D

Thanks for your help @testforstephen - perhaps this should be documented somewhere in the readme? Or would java developers actually understand the relationship here? :)

@yusijs thanks to let us know the workaround works.

We're trying to add a new preference that disables it by default, or at least asks user whether to enable null analysis feature.

@testforstephen
Copy link
Contributor

@yusijs Would you mind sharing a minimum sample project to help us reproduce the issue? It's necessary to include the annotation definition and usage part. We want to investigate the exact root case and fix this regression eventually.

@yusijs
Copy link
Author

yusijs commented Nov 1, 2022

Hi, sorry @testforstephen, I completely forgot to answer you here! I can't really share a lot more than what I have in the original description, but I can give you this class where the compilation actually fails:

package com.equinor.gom.model.validator.annotation;

import com.equinor.gom.model.validator.AssetAreaValidator;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {AssetAreaValidator.class}) // This line was where the error would be thrown
public @interface ValidateAssetArea {

    /**
     * An attribute message that returns the default key for creating error messages in case the constraint is violated.
     *
     * @return message string with a description of constraint triggered
     */
    String message() default "invalid";

    /**
     * An attribute groups that allows the specification of validation groups, to which this constraint belongs. This must default to an empty array of type
     * Class<?>.
     *
     * @return attribute groups
     */
    Class<?>[] groups() default {};

    /**
     * An attribute payload that can be used by clients of the Bean Validation API to assign custom payload objects to a constraint.
     *
     * @return attribute payload
     */
    Class<? extends Payload>[] payload() default {};

}

@timvahlbrock
Copy link

I have a similar problem in https://github.com/timvahlbrock/miniJavaParser, which uses JavaCC to generate Java sources. Compilation and tests succeed in maven, but this vscode extension runs into build failures. The logged problems declares the imports from the generated imports as unresovable. I tried using the workaround described above, but without success. Should I make this into a new issue or is this related to this?

@timvahlbrock
Copy link

Nvm, I think my problem is caused by redhat-developer/vscode-java#177

@mathieufortin01
Copy link

Exact same issue here, for quite some time. Each new project refresh brought their share of those compilation issues. As soon as i clicked each problem they disappeared. That was my goto method UNTIL i found this ticket. Disabling null annotation feature just fixed it.

@hexhoc
Copy link

hexhoc commented Jul 5, 2024

I spent 2 days trying to solve this problem until I realized that I needed to disable the option - nullAnalysis. I'm crying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants