Code review comment: Can you briefly explain how changing the order fixes the issue?

Code under review:
public void visit(BLangPackage pkgNode) {
        if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
            result = pkgNode;
            return;
        }
        observabilityDesugar.addObserveInternalModuleImport(pkgNode);

        code2CloudDesugar.addCode2CloudModuleImport(pkgNode);

        createPackageInitFunctions(pkgNode, env);
        
        addAttachedFunctionsToPackageLevel(pkgNode, env);

        if (!pkgNode.testablePkgs.isEmpty() && pkgNode.getTestablePkg().getMockFunctionNamesMap() != null) {
            mockDesugar.generateMockFunctions(pkgNode);
        }

        
        closureGenerator.visit(pkgNode);

        
        annotationDesugar.initializeAnnotationMap(pkgNode);

        pkgNode.constants.stream()
                .filter(constant -> (constant.expr.getKind() == NodeKind.LITERAL ||
                        constant.expr.getKind() == NodeKind.NUMERIC_LITERAL)
                        && constant.expr.getBType().tag != TypeTags.TUPLE)
                .forEach(constant -> pkgNode.typeDefinitions.add(constant.associatedTypeDefinition));

        BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env);
        BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body;

        rewriteConstants(pkgNode, initFnBody);

        pkgNode.constants = removeDuplicateConstants(pkgNode);

        rewrite(pkgNode.typeDefinitions, env);

        pkgNode.globalVars = desugarGlobalVariables(pkgNode, initFnBody);

        pkgNode.services.forEach(service -> serviceDesugar.engageCustomServiceDesugar(service, env));

        annotationDesugar.rewritePackageAnnotations(pkgNode, env);

        
        addUserDefinedModuleInitInvocationAndReturn(pkgNode);

        
        pkgNode.typeDefinitions.sort(Comparator.comparing(t -> t.precedence));

        pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
        pkgNode.constants = rewrite(pkgNode.constants, env);
        pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
        pkgNode.classDefinitions = rewrite(pkgNode.classDefinitions, env);

        serviceDesugar.rewriteListeners(pkgNode.globalVars, env, pkgNode.startFunction, pkgNode.stopFunction);
        ASTBuilderUtil.appendStatements(serviceAttachments, (BLangBlockFunctionBody) pkgNode.initFunction.body);

        addNilReturnStatement((BLangBlockFunctionBody) pkgNode.startFunction.body);
        addNilReturnStatement((BLangBlockFunctionBody) pkgNode.stopFunction.body);

        if (isJvmTarget) {
            largeMethodSplitter.splitLargeFunctions(pkgNode, env);
        }

        pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
        pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
        pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);

        for (int i = 0; i < pkgNode.functions.size(); i++) {
            BLangFunction function = pkgNode.functions.get(i);
            if (!function.flagSet.contains(Flag.LAMBDA) || function.name.value.contains(MockDesugar.MOCK_FUNCTION)) {
                pkgNode.functions.set(i, rewrite(pkgNode.functions.get(i), env));
            }
        }

        for (BLangLambdaFunction lambdaFunction : pkgNode.lambdaFunctions) {
            rewrite(lambdaFunction, lambdaFunction.capturedClosureEnv);
        }

        
        closureDesugar.visit(pkgNode);

        for (BLangTestablePackage testablePkg : pkgNode.getTestablePkgs()) {
            rewrite(testablePkg, this.symTable.pkgEnvMap.get(testablePkg.symbol));
        }
        pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
        result = pkgNode;
    }

Line commented on: rewrite(pkgNode.typeDefinitions, env);

Revised code:
public void visit(BLangPackage pkgNode) {
        if (pkgNode.completedPhases.contains(CompilerPhase.DESUGAR)) {
            result = pkgNode;
            return;
        }
        observabilityDesugar.addObserveInternalModuleImport(pkgNode);

        code2CloudDesugar.addCode2CloudModuleImport(pkgNode);

        createPackageInitFunctions(pkgNode, env);
        
        addAttachedFunctionsToPackageLevel(pkgNode, env);

        if (!pkgNode.testablePkgs.isEmpty() && pkgNode.getTestablePkg().getMockFunctionNamesMap() != null) {
            mockDesugar.generateMockFunctions(pkgNode);
        }

        
        closureGenerator.visit(pkgNode);

        
        annotationDesugar.initializeAnnotationMap(pkgNode);

        pkgNode.constants.stream()
                .filter(constant -> (constant.expr.getKind() == NodeKind.LITERAL ||
                        constant.expr.getKind() == NodeKind.NUMERIC_LITERAL)
                        && constant.expr.getBType().tag != TypeTags.TUPLE)
                .forEach(constant -> pkgNode.typeDefinitions.add(constant.associatedTypeDefinition));

        BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env);
        BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body;

        rewriteConstants(pkgNode, initFnBody);

        pkgNode.constants = removeDuplicateConstants(pkgNode);

        pkgNode.globalVars = desugarGlobalVariables(pkgNode, initFnBody);

        pkgNode.services.forEach(service -> serviceDesugar.engageCustomServiceDesugar(service, env));

        annotationDesugar.rewritePackageAnnotations(pkgNode, env);

        
        addUserDefinedModuleInitInvocationAndReturn(pkgNode);

        
        pkgNode.typeDefinitions.sort(Comparator.comparing(t -> t.precedence));

        pkgNode.typeDefinitions = rewrite(pkgNode.typeDefinitions, env);
        pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env);
        pkgNode.constants = rewrite(pkgNode.constants, env);
        pkgNode.globalVars = rewrite(pkgNode.globalVars, env);
        pkgNode.classDefinitions = rewrite(pkgNode.classDefinitions, env);

        serviceDesugar.rewriteListeners(pkgNode.globalVars, env, pkgNode.startFunction, pkgNode.stopFunction);
        ASTBuilderUtil.appendStatements(serviceAttachments, (BLangBlockFunctionBody) pkgNode.initFunction.body);

        addNilReturnStatement((BLangBlockFunctionBody) pkgNode.startFunction.body);
        addNilReturnStatement((BLangBlockFunctionBody) pkgNode.stopFunction.body);

        if (isJvmTarget) {
            largeMethodSplitter.splitLargeFunctions(pkgNode, env);
        }

        pkgNode.initFunction = rewrite(pkgNode.initFunction, env);
        pkgNode.startFunction = rewrite(pkgNode.startFunction, env);
        pkgNode.stopFunction = rewrite(pkgNode.stopFunction, env);

        for (int i = 0; i < pkgNode.functions.size(); i++) {
            BLangFunction function = pkgNode.functions.get(i);
            if (!function.flagSet.contains(Flag.LAMBDA) || function.name.value.contains(MockDesugar.MOCK_FUNCTION)) {
                pkgNode.functions.set(i, rewrite(pkgNode.functions.get(i), env));
            }
        }

        for (BLangLambdaFunction lambdaFunction : pkgNode.lambdaFunctions) {
            rewrite(lambdaFunction, lambdaFunction.capturedClosureEnv);
        }

        
        closureDesugar.visit(pkgNode);

        for (BLangTestablePackage testablePkg : pkgNode.getTestablePkgs()) {
            rewrite(testablePkg, this.symTable.pkgEnvMap.get(testablePkg.symbol));
        }
        pkgNode.completedPhases.add(CompilerPhase.DESUGAR);
        result = pkgNode;
    }