stan-dev / stan-dev/rstan

RStan yields incorrect line numbers in compiler and runtime errors

Open
#1,123 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.1k
Forks
266
Avg merge
2h 56m
Merged PRs (30d)
1

Description

Description:

It has come up several times on the forums that the line numbers reported by RStan are incorrect. The most recent is here:
https://discourse.mc-stan.org/t/stan-error-messages/34820

The reportee (@cdriveraus) provided a script which shows this by inserting a bad line at a known position in the source and showing what the compiler reports:

library(curl)

# URL of the raw file on GitHub
github_raw_url <- "https://raw.githubusercontent.com/cdriveraus/ctsem/master/inst/stan/ctsm.stan"

# Read the file content directly from the URL
stanmodeltext <- readLines(curl(github_raw_url))
line_number <- c()
# Split the code into lines
for(linei in 800:1000){
  # code_lines <- strsplit(stanmodeltext, "\n")[[1]]
  
  # Generate a random line of code
  random_line <- "function(x);"
  
  # Insert the random line at a random position
  insert_position <- linei#sample(length(code_lines), 1)
  modified_code <- append(stanmodeltext, random_line, after = insert_position - 1)
  
  # Join the modified lines back into a single string
  modified_stan_code <- paste(modified_code, collapse = "\n")
  Sys.sleep(.02)
  error_string=try(rstan::stan_model(model_code = modified_stan_code))
  #extract the string " , line xxx' from the error message
  line_number <- c(line_number,as.numeric(gsub(".*line (\\d+).*", "\\1", error_string)))
    
    plot(1:length(line_number),line_number)
    abline(min(line_number),1)
}

image

I have modified the above code to use both cmdstanr and raw V8 calls to stancjs, and both times have gotten a plot which was a straight line:

image

Possible causes

Rstan does a lot of preprocessing on files before passing them to stanc3js:
https://github.com/stan-dev/rstan/blob/50c3c6f4e44a849b17af7e9e2b8656cd93238a18/rstan/rstan/R/stanc.R#L62-L90

Oddly enough, the ctsm file which is used in the above example contains no #includes, so I would not expect it to be running.

I believe the same preprocessing is what leads to https://github.com/stan-dev/rstan/issues/1067

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with rstan/R/stanc.R lines 62-90 and reproduce the reported script using rstan::stan_model, comparing inserted source lines with compiler and runtime error locations. Done means the reported line numbers match the corresponding lines in the original Stan source, including the demonstrated preprocessing cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.