dnscontrol icon indicating copy to clipboard operation
dnscontrol copied to clipboard

Typescript autocomplete puts errors on require calls

Open labrown opened this issue 9 months ago • 8 comments

Describe the bug After running dnscontrol write-types I noticed that the block of require calls in my main dnscontrol.js file was marked with errors in vscode:

Image

When I mouse over the marked text I see this error message:

Image

I expect this is fallout from re-using the require command from javascript as part of the dnscontrol DSL.

To Reproduce Steps to reproduce the behavior:

  1. Install dnscontrol
  2. Follow instructions to install the typescript autocomplete files and activate them in a dnscontro.js file
  3. Add a require("file") call to your dnscontro.js
  4. See the incorrect error text

A clear and concise description of what you expected to happen:

The typescript autocomplete should handle the require top-level command properly, not through an inappropriate error message.

labrown avatar Mar 25 '25 03:03 labrown

require is a special keyword; using the require function causes typescript to infer that this file is a module, thus losing the ability to infer global variables. If you need this functionality, here's a temporary solution:

var require2 = require;
require2("./zones/example.com.js");

In addition, you can create a tsconfig.json file that hints the typescript with correct runtime, which has better inferencing capabilities. My tsconfig.json

{
  "compilerOptions": {
    "lib": ["es5"],
    "allowJs": true,
    "checkJs": true,
    "module": "None",
    "strict": true,
    "noEmit": true,
  },
  "include": [
    "zones/**/*.js",
    "dnsconfig.js",
    "types-dnscontrol.d.ts",
  ]
}

huihuimoe avatar Mar 25 '25 05:03 huihuimoe

I'm aware require is a special keyword, which is why I mentioned the bit about it being a javascript command.

I tried putting your tsconfig.json file in the root dir of my dnscontrol setup and then loaded dnscontrol.js into vscode but it is still showing the incorrect error. I am not a javascript/typescript programmer so I'm just banging around here. How does one use a tsconfig.json file?

labrown avatar Mar 25 '25 05:03 labrown

just a reference for you.

my-domains
├── .gitignore
├── .gitlab-ci.yml
├── creds.json
├── dnsconfig.js
├── spfcache.json
├── tsconfig.json
├── types-dnscontrol.d.ts
└── zones
    ├── example.com.js
    ├── other...

tsconfig.json should include all your js/ts code.

...
  "include": [
    "zones/**/*.js",         // <- all zones
    "dnsconfig.js",          // <- entry point
    "types-dnscontrol.d.ts", // <- types file
  ]
}

dnsconfig.js

var REG_NONE = NewRegistrar("none");
var DSP_CLOUDFLARE = NewDnsProvider("cloudflare");

require_glob("./zones/", false);

// or require a single file manually
// var require2 = require;
// require2("./zones/example.com.js");

example.com.js

D("example.com", REG_NONE,
  DnsProvider(DSP_CLOUDFLARE),
  TXT("@", "example"),
);

huihuimoe avatar Mar 25 '25 06:03 huihuimoe

And as it turns out, the types-dnscontrol.d.ts info for require_glob incorrectly demands the second argument, recursive, which can be left off to take its default value as well. I discovered that while working through your example.

labrown avatar Mar 25 '25 06:03 labrown

I'm not an expert but I think its a matter of editing documentation/language-reference/top-level-functions/require_glob.md to mark that as optional. that is, change recursive: boolean to recursive: boolean?

To test:

go generate
go build
dnscontrol write-types

Test the new types-dnscontrol.d.ts file.

tlimoncelli avatar Mar 25 '25 19:03 tlimoncelli

Closed by https://github.com/StackExchange/dnscontrol/pull/3508.

cafferata avatar Mar 27 '25 18:03 cafferata

@cafferata The fix for require_glob doesn't fix the mistaken typescript error for the require call. Did you close this because that can't be fixed?

labrown avatar Mar 27 '25 23:03 labrown

Re-opening.

tlimoncelli avatar Mar 28 '25 14:03 tlimoncelli